如何从 find() 获取数据以呈现网站

编程入门 行业动态 更新时间:2024-10-04 13:21:33

如何从 find() 获取数据以呈现<a href=https://www.elefans.com/category/jswz/34/1771036.html style=网站"/>

如何从 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() 获取数据以呈现网站

本文发布于:2024-05-31 01:04:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网站   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!