Reputation: 3606
In the code below using var updated to count updates, but getting zero instead
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);
});
All is fine except counting, see result below.
Pinged: 157
Process finished with exit code 0
Upvotes: 0
Views: 34
Reputation: 577
How is this supposed to work? process.exit is called before any of the asynchronous targets.forEach calls is made. You need to control the flow, e.g. with async, and not exit the process before all of the targets.forEach calls is finished.
Upvotes: 1