NodeJs:使用js命令文件执行mongoshell

时间:2016-11-08 11:55:21

标签: javascript node.js mongodb shell

我写了一个nodeJS脚本,它有一组mongo命令。我使用以下命令在终端中运行它并且它可以工作:

mongo --host 127.0.0.1:27017 data.js

它奏效了,它执行了所有需要完成的操作。

之后,我创建了另一个nodeJS文件,以自动执行此脚本。我使用exec依赖项执行相同的命令。

var exec = require("child_process").exec;
var execString = "mongo --host 127.0.0.1:27017 " + config.file;
exec(execString, function(err, res){

    if (err) {
        return cb(err);
        console.log("here")
    }
    else
        console.log("Data import is Successfully Done");
    });

config.file包含js文件的路径。这是我收到的错误

{ Error: Command failed: mongo --host 127.0.0.1:27017 /opt/soajs/node_modules/soajs.utilities/data/artifact/data.js

at ChildProcess.exithandler (child_process.js:211:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:501:12)
killed: false,
code: 253,
signal: null,
cmd: 'mongo --host 127.0.0.1:27017 /opt/soajs/node_modules.  /soajs.utilities/data/artifact/data.js' }

0 个答案:

没有答案