您能向我解释为什么我无法访问db.collection('mains')范围之外的if块生成的值。在全局范围内存储结果的最佳方法是什么?
var foo = function(){
let res = [];
db.collection('mains').find().toArray((error, result) => {
if (error) throw error;
result.forEach((result) => {
session.pingHost(result.ip, (error, target) => {
if(error){
console.log(`Can't contact: ${target} timeStamp: ${getDateTime(true)}`);
res.push(target);
} else {
console.log(`${target}: OK tag: ${getDateTime(false)}`);
res.push(target);
};
});
});
});
return res;
};
console.log(foo());