【发布时间】:2015-11-10 12:40:44
【问题描述】:
Product.find() 的承诺在更新最高价格的循环完成之前解决。仅当数据操作完成后,我如何才能解决承诺?
Product.find({'prodType': req.params.type}).lean().exec(function (err, product) {
if (err) {
res.status(400);
}
for (var i = 0; i < product.length; i++) {
(function(u) {
Option.find({'prodId': product[i].productId}).lean().sort({price: -1}).limit(1).exec(function (err, highest) {
product[u].price = highest;
// not updated in returned object
});
})(i);
}
}).then(function (product) {
res.send(product);
});
【问题讨论】: