如何使这个垃圾变量及时读取为shipmentObjs的值?我知道console.log会读取“undefined”但我希望它在函数后执行。怎么样?我尝试了几种方法,似乎无法找到实现这一目标的方法
let garbage;
db.scan({TableName: 'shipments'}, (err,shipments) => {
if (err) console.error(err);
// We got our shipments
let shipmentObjs = shipments.Items;
// Bring out the "done: false" shipments to display
shipmentObjs = shipmentObjs.filter(obj => obj.done === false);
garbage = shipmentObjs;
});
});
// prints "undefined" . Must figure out a way to make it read shipmentObjs from the above code
console.log(garbage);
答案 0 :(得分:0)
console.log(garbage);
要完成上面的代码,你需要将它放在db.scan
函数中。
答案 1 :(得分:0)
两件事。首先,如果你正在调用异步函数 - db.scan()是什么,顺序无关紧要。正如@Amresh所说,为此使用承诺或回调。您的代码的第二个问题是在内部 db.scan()func中发生了什么。我不知道你正在使用什么db库,但如果你收到错误,很可能你不会收到任何架构。因此,您的代码会在shipmentObjs.filter
尝试时崩溃。你需要照顾这个部分。只需返回错误。