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?
child_process. Since you are still working in the node, just makerequire(mongodb-migrate), and callsetDbConfig(<Ваш JSON>)and thenrun('up')- Yaantgrunttask, 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. - SkyDancerrunit is possible to pass a revision number for a type of migration like this sorun('up', 10)or is this the argument ofmigrationEndresponsible?function runMongoMigrate(direction, migrationEnd, next)- SkyDancer