0

I cannot update my collection even without encountering any error. Can someone help? Please I am troubleshooting this for like 3 hours now.

const product_id = req.body.cartItems.product_id;
      const item = cart.cartItems.find(c => c.product_id == product_id);
      if (item) {
        Cart.findOneAndUpdate({ "user": req.user._id, "cartItems.product_id" : product_id }, {
            "$set": {
              "cartItems.$":{
                ...req.body.cartItems.quantity,
                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 });
          }
        });

The MODEL:

const mongoose = require('mongoose');

const cartSchema = new mongoose.Schema({
    user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true},
    cartItems: [
        {
            product_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true}, // This foreign key to product _id
            quantity: {type: Number,required: true},
            price: {type: Number, required: true}
        }
    ]
}, {timestamps: true});

module.exports = mongoose.model('Cart', cartSchema);
4
  • Can you provide your mongoose model? Commented Apr 7, 2021 at 15:02
  • 1
    You can edit your post and insert it there, too! Commented Apr 7, 2021 at 15:29
  • You may now check it @stefOb sorry I'm still a newbie on stackoverflow Commented Apr 7, 2021 at 15:33
  • I´ve added a solution, I hope it solves your issue. Commented Apr 7, 2021 at 15:43

2 Answers 2

1

You need to use the $[] operator to update all documents inside the cartItems array matching your filter criteria. This operator acts like a placeholder for all items in the array. Considering your coding it would be something like this:

Cart.findOneAndUpdate({ "user": req.user._id, "cartItems.product_id" : product_id }, {
        "$set": {
          "cartItems.$[].quantity": item.quantity + req.body.cartItems.quantity
        }
      }).exec(...)
Sign up to request clarification or add additional context in comments.

1 Comment

I'll test this thank you so much! Atleast I have the idea now
0
const product = req.body.cartItems.product;
const items = cart.cartItems.find((c) => c.product == product);

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

    if (_cart) {
      res.status(200).json({
        cart: _cart,
      });
    }
  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.