网站"/>
如何从 find() 获取数据以呈现网站
User.find({}, function(error, cartUser){
if (error) {
const error = new Error(err);
error.httpStatusCode = 500;
return next(error);
}
let cartUserArray = []
cartUser.forEach(u => {
if(u.cart.items.length > 0){
u
.populate('cart.items.productId')
.execPopulate()
.then(user => {
cartUserArray.push(user)
console.log("cartUserArray 1" + cartUserArray);
return cartUserArray
})
console.log("cartUserArray 2" + cartUserArray);
return cartUserArray
}
})
console.log("cartUserArray 3" + cartUserArray);
const products = cartUserArray
res.render('shop/cart', {
path: '/cart',
pageTitle: 'Your Cart',
products: products
});
})
Result
cartUserArray 2
cartUserArray 2
cartUserArray 3
cartUserArray 1{
cart: { items: [ [Object], [Object] ] },
role: 'user',
_id: 645fb212aa2e1cd207052f6b,
name: 'user1',
email: '[email protected]',
phone: 1234567,
password: '$2a$12$jEfix45P2PmFDZQeRsg4/e5GnZgmtnYxNFYk5wotvbMM3pTgZN8JK',
__v: 0
}
cartUserArray 1{
cart: { items: [ [Object], [Object] ] },
role: 'user',
_id: 645fb212aa2e1cd207052f6b,
name: 'user1',
email: '[email protected]',
phone: 1234567,,
password: '$2a$12$jEfix45P2PmFDZQeRsg4/e5GnZgmtnYxNFYk5wotvbMM3pTgZN8JK',
__v: 0
}
您好,我想问一下,为什么我的日志“cartUserArray 3”取不到数据? 我想将“cartUserArray”放入“const products”并将其呈现到网站上。 但似乎我无法在 foreach 函数之外获取数据。 谢谢你的帮助
回答如下:更多推荐
如何从 find() 获取数据以呈现网站
发布评论