Javascript 如何使用mongoose更新MongoDB中的数组?
我有两个模式,用户和产品。用户模式是我存储所有产品ID和用户添加到购物车的项目数的地方 当用户请求“/checkout”时,它应该更新数量,然后将其从购物车中删除。当我检查数量时,我遇到了一个问题,即没有更新数量Javascript 如何使用mongoose更新MongoDB中的数组?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我有两个模式,用户和产品。用户模式是我存储所有产品ID和用户添加到购物车的项目数的地方 当用户请求“/checkout”时,它应该更新数量,然后将其从购物车中删除。当我检查数量时,我遇到了一个问题,即没有更新数量 router.post('/checkout', auth, catchAsync(async (req, res) => { const user = await User.findById(req.session.userId); const err = [
router.post('/checkout', auth, catchAsync(async (req, res) => {
const user = await User.findById(req.session.userId);
const err = [];
if (user && user.products.length > 0) {
user.products.map(async (product) => {
let total = 0;
const p = await Product.findById(product.productID);
if (p.quantity > 0 && p.quantity > product.quantity) {
console.log('IN');
total = p.quantity - product.quantity;
console.log(total);
await Product.findOneAndUpdate({ _id: product.productID }, { $set: { quantity: total } });
} else {
err.push(`Item, ${p.name} is sold out`);
}
});
await User.findOneAndUpdate({ _id: req.session.userId }, { $set: { products: [] } });
if (err.length) {
return res.status(500).json({ message: err });
}
return res.status(200).json({ message: 'OK' });
}
return res.status(200).json({ message: 'Empty cart' });
}));
用户架构:
产品架构:
我相信代码中的问题在于
user.products.map(…)
函数,因为您从不等待在map中创建的所有承诺得到解决
换句话说,map
函数返回一组挂起的承诺,但它不会等待这些承诺完成,因此,在执行map
中的任何代码之前,执行将继续执行到res.status(…)
您有不同的选项来解决它,但主要需要处理map
函数返回的承诺数组,并在结束代码之前等待它们的完成。关于如何使用async/await
at处理这种情况,有一个非常好的解释
我通常利用Promise.all()
函数,该函数从承诺数组中返回一个承诺,因此您可以等待map
中的代码对数组中的每个项目并行执行(即,您的情况下,产品
)。你可以在网站上阅读更多关于它的信息
/。。。
让promisesArray=user.products.map(异步产品=>{…});
//promisesArray应该看起来像:[Promise{},Promise{},…]
//使用Promise.all,我们等待它们并行完成
等待承诺。一切(承诺);
//现在,您可以确定映射中的代码已针对每个产品执行
// ...
一个很好的做法是在
Promise.all()
周围使用try{}catch(err){}
块来处理某些承诺被拒绝的情况。您能分享产品集合模式吗。@Jitendra我刚刚更新了帖子