Search code examples
javascripte-commerce

Javascript TypeError: Cannot read property 'find' of undefined


Good day, I'm new to coding & facing a weird problem that I can't solve by my own, please see my code below.

const Cart = require('../model/cart');
 
    exports.addToCart = (req, res) => {
        Cart.find({user: req.user._id}).exec((error, data) => {
            if (error) return res.send(error);
            if (data) {
                console.log(data);
                const product = req.body.cartItems.product;
                const addOne = data.cartItems.find(c=>c.product == product);
                if(addOne){
                    Cart.findOneAndUpdate({ "user" : req.user._id, "cartItems.product" :product},{   
                        "$set":{
                            "cartItems": {
                                ...req.body.cartItems,
                                quantity : addOne.qunatity + req.body.cartItems.qunatity,
                            }
                        }
                    }).exec((error,data)=>{
                        if (error) return res.send(error);
                        if (data) return res.send(data);
                    });
                }
                else{
                    Cart.findOneAndUpdate({ user : req.user._id},{
    
                        "$push":{
                            "cartItems": req.body.cartItems
                        }
                    }).exec((error,data)=>{
                        if (error) return res.send(error);
                        if (data) return res.send(data);
                    });
                }        
            }
            else {
    
                const cart = new Cart({
                    user: req.user._id,
                    cartItems: [req.body.cartItems]
                });
                cart.save((error, data) => {
                    if (error) return res.send(`Something went wrong ${error}`);
    
                    if (data) return res.send(data);
                }) 
            }
        })
    };
  1. I am getting an error on

    const addOne = data.cartItems.find(c=>c.product == product);

  2. But by my logic code should not go inside the if(data) condition because the data is empty for the first entry. It should go to the bottom to the else code.


Solution

  • But by my logic code should not go inside the if(data) condition because the data is empty for the first entry. It should go to the bottom to the else code.

    Your if statement condition doesn't save you from your error TypeError: Cannot read property 'find' of undefined because "data" contains any kind of value (exclude -> null, undefined or 0) but your expected "cartItems" don't exist at your "data" object.

    You can try following code snipets to debug your code:

    console.log(data.cartItems) // will return undefined
    console.log(data) // doesn't contain "cartItems"
    

    If cartItems is important for your condition use:

    if(data && data.cartItems)