我有以下代码,它将许多functionCalls传递给各种服务器。在返回所有functionCalls时,响应对象被发送到Mongo数据库并存储为单个文档。承诺的原因是所有函数必须完成并且它们的返回值在响应部分中,以便数据库可以存储所有适当的响应。 (代码如下)
Q.all(functionCalls)
.then(function(response) {
//console.log(response);
Q.then(dbHandler(response))
.catch( function(e) {
console.log(e);
})
.finally( function() {
console.log('Execution completed successfully!');
});
}, function() {
console.log('unhandled exception');
});
我遇到的问题是涉及ssh-promise库的单个函数调用。问题是在响应中填充stdout或stderror字段之前解析了promise。这是ssh调用的代码。简短的代码如下:
var ssh = new Client(config);
var ack = "200 - OK";
var pub = {};
var execString = "java -classpath" + classpath + javaSecurityArgs + " -DAPP_ENV=" + env + javaMainClass + jobString + " date=" + seconds;
ssh.exec( execString )
.then( function(stdout) {
json.parse(stdout.match(/^FulfillmentReportJson:(.*)$/gm))
.catch(function (e) {
console.log(e.message);
pub = {
'type': 'fulfillmentBatch',
'result': 'failed'
};
})
.then(function (response) {
console.log(stdout);
pub = {
'type': 'fulfillmentBatch',
'result': response
};
}
)
})
.catch(function(stderr) {
pub = {
'type': 'fulfillmentBatch',
'result': 'failed' + stderr
};
})
.done();
“then”的promise履行返回stdout或stderror的响应,并将其填充到返回的pub.result中。
问题是:当我执行此代码时,承诺在ssh.exec返回stdout或stderror之前完成。这就像承诺完成,然后差不多5秒钟(在数据库对象写完之后),我们看到了stdout / stderror数据的返回。数据库对象包含来自此函数应填充数据的位置的“null”,并且“then”部分中的console.log(stdout)包含stdout / stderror数据。任何帮助将不胜感激。
答案 0 :(得分:0)
我在上面的响应之后检查了代码,意识到我正在接近ssh命令错误。
ssh.exec(execString, {
exit: function(code, stdout, stderr) {
我应该一直在使用'退出'而不是' out'和#err'对执行官的回应。出口允许构建适当的响应对象