【发布时间】:2020-09-12 17:56:00
【问题描述】:
我正在查阅一份文件,结果我查阅了另一份文件model,但最后,什么都没有返回给我,foreach 不等待await。
ShoppingCart.find({
"userId": id
}).then(async ShoppingCart => {
let distinctTypes = ShoppingCart[0].productsCart;
distinctTypes.sort(function(r, u) {
return r.manufacturerId > u.manufacturerId ? 1 : r.manufacturerId < u.manufacturerId ? -1 : 0
});
let products = [];
let data2 = await distinctTypes.forEach(async function(thisType) {
let id = thisType.productId;
let data = await Product.findById(id).then(Product => {
thisType.productId = Product;
products.push(thisType);
return products;
});
return data; ///at this point the information is correct
});
return data2;
});
“数据”:空
【问题讨论】:
标签: javascript node.js mongodb mongoose async-await