在下面的代码中使用 var Updated 来计数更新,但得到零
var updated = 0;
Hosts.find({"model": "cp11"}, {"address":1, _id:0}, function (err, targets) {
if (err) return console.error(err);
console.log("Pinged: " + targets.length);
targets.forEach(function (host) {
ping.promise.probe(host.address, {
timeout: 1
//extra: ["-i 5"]
}).then(function (res) {
if (res.alive) {
Hosts.update({"address": res.host},{"$set": output},{upsert: true}, function(err){
if (err) console.log(err);
});
++updated;
}
});
});
process.exit(updated);
});
除了计数之外一切都很好,请参见下面的结果。
Pinged: 157
Process finished with exit code 0
最佳答案
这应该如何运作? process.exit 在任何异步 Target.forEach 调用之前调用。您需要控制流量,例如使用异步,并且在所有 Targets.forEach 调用完成之前不退出进程。
关于javascript - node.js 变量 'updated' 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30017534/