There is a database migration library (mongo-migrate - https://github.com/afloyd/mongo-migrate ), it starts with the following line in the nodejs terminal: node ./node_modules/mongodb-migrate -runmm -dbc '{"host":"localhost","db":"myDb","port":27017,"username":"user1","password":"pswd1"}' up I need to run this command inside the js-file, I try to use require('child_process').exec , But I can not understand how I pass this JSON as a parameter in exec ... I tried this:

 let exec = require('child_process').exec; exec(`node ./node_modules/mongodb-migrate -runmm -dbc '{"host":"localhost","db":"myDb","port":27017,"username":"user1","password":"pswd1"}' up`, (error, stdout, stderr) => { if (error) { console.error(`exec error: ${error}`); return; } console.log(`${stdout}`); console.log(`${stderr}`); }); 

But I get the error:

 exec error: Error: Command failed: node ./node_modules/mongodb-migrate -runmm -dbc '{"host":"localhost","db":"myDb","port":27017,"username":"user1","password":"pswd1"}' up undefined:1 '{host:localhost,db:myDb,port:27017,username:user1,password:pswd1}' ^ SyntaxError: Unexpected token h in JSON at position 0 

It feels like this exec formats my source string like that and removes everything " , and the output is JSON invalid ...

How do I pass JOSN as argupent to require('child_process').exec that it is unchanged?

  • one
    So it is not necessary to run through child_process . Since you are still working in the node, just make require(mongodb-migrate) , and call setDbConfig(<Ваш JSON>) and then run('up') - Yaant
  • Yes, it works if you just run the script, but if this is all written inside the grunt task, then for some reason it does not work .... So far, I don’t understand why ... I’m trying to figure it out, and everything seems to be working fine, thanks for the reply. - SkyDancer
  • @Yaant And in this run it is possible to pass a revision number for a type of migration like this so run('up', 10) or is this the argument of migrationEnd responsible? function runMongoMigrate(direction, migrationEnd, next) - SkyDancer
  • I don’t know, I don’t even know what these same migrations are, for the previous comment this knowledge was not required. :) Judging by the code, this parameter allows you to specify the number of the required migration (whatever that means). - Yaant
  • @Yaant, Yes, that's how it was, there was an error on my side already, so all the rules of ATP :) - SkyDancer

0