【问题标题】:I keep getting TypeError: Cannot read property 'find' of undefined我不断收到 TypeError: Cannot read property 'find' of undefined
【发布时间】:2021-10-20 10:21:09
【问题描述】:

我目前正在开发我的添加到购物车控制器,我是否遗漏了任何导致它无法工作的东西?似乎我的“cartItems”没有定义,我该如何定义?抱歉,我是初学者。我的 /controller/cart 下有以下内容:

const Cart = require('../models/cart');

exports.addItemToCart = (req, res) => {

    Cart.find({ user: req.user._id})
    .exec( (error, cart) => {
        if(error) return res.status(400).json({ error });
        if(cart){
            //if cart already exists then update cart by quantity

            const product = req.body.cartItems.product;
            const item = cart.cartItems.find(c => c.product == product); //ERROR OCCURS HERE//

            if(item){

                Cart.findOneAndUpdate({ user: req.user._id, "cartItems.product": product}, {
                    "$set": {
                        "cartItems": {
                            ...req.body.cartItems,
                            quantity: item.quantity + req.body.cartItems.quantity
                        }
                    }
                })
                .exec( ( error, _cart) => {
                    if(error) return res.status(400).json({ error });
                    if(_cart){
                        return res.status(201).json({ cart: _cart})
                    }
                })

            } else {
                Cart.findOneAndUpdate({ user: req.user._id }, {
                    "$push": {
                        "cartItems": req.body.cartItems
                    }
                })
                .exec( ( error, _cart) => {
                    if(error) return res.status(400).json({ error });
                    if(_cart){
                        return res.status(201).json({ cart: _cart})
                    }
                })
            }

           

        } else {
            //if cart does not exist then create a new cart
            const cart = new Cart({
                user: req.user._id,
                cartItems: [req.body.cartItems]
            });

            cart.save( (error, cart) => {
                if(error) return res.status(400).json({ error });
                if(cart){
                    return res.status(201).json({ cart });
                }
            }); 
        }
    });

}

我的购物车架构如下:

const mongoose = require('mongoose');
const cartSchema = new mongoose.Schema({

    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    cartItems: [
        {
            product: { 
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Product',
                required: true
            },
            quantity: {
                type: Number,
                default: 1
            },
            price: {
                type: Number,
                required: true
            }
        }
    ]
}, { timestamps: true });


module.exports = mongoose.model('Cart', cartSchema);

当我尝试在邮递员中运行一个发布请求时:

{
    "cartItems": {
        "product": "611d12946c14c2232c933af8",
        "quantity": 1,
        "price": 7000
    }
}

我在 gitbash 上收到此错误:

events.js:288
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read property 'find' of undefined
    at cart.js:12:41

【问题讨论】:

  • 您的cart 是一个数组。如果你想要一个对象,请改用Cart.findOne(...
  • @CuongLeNgoc 我也试过这个,但它不起作用

标签: javascript node.js mongoose


【解决方案1】:

您的_id 是否已定义?从用户结构看不像。我会在第 12 行之前添加一个console.log(req),这样您就可以查看入站参数中的内容,并确定您的处理程序假设中缺少哪些部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 2019-01-11
    • 2020-09-20
    • 2021-06-25
    • 2021-09-25
    • 2021-09-25
    相关资源
    最近更新 更多