How can i update the product quantity in mongodb?

  Kiến thức lập trình

0

I am trying to add a product to a shopping cart in mongodb but the code I have written does not work. I have set up and if statement to check if the selected product’s id is included in the items array and if true to console.log(‘match found’); within this if statement is also where my code that is not working is set up. Can you show me a better way to update the product quantity instead of whats it’s doing now which is just adding the same product to cart in mongodb.

const express = require('express');
const Carts = require('../repo/carts');

const router = express.Router();

router.post('/cart/products', async (req, res) => {
  Carts.findById(req.session.cartId, (err, foundCart) => {
    if (err) {
      console.log(err); // This err is for the find by Id, not to the update function
    }
    if (foundCart) {
      console.log(foundCart);
      console.log(req.body.productId);

      let check = foundCart.items.map((item) => item.id);
      // console.log(check);
      //THE CODE BELOW IS WHERE ISSUE OCCURS
      if (check.includes(req.body.productId)) {
        console.log('MATCH FOUND');
        Carts.updateOne(
           {_id: req.body.productId },
           { $inc: {  quantity: 1 } }
        );
      }

      Carts.updateOne(
        { _id: foundCart._id },
        {
          $push: {
            items: {
              _id: req.body.productId,
              quantity: 1,
            },
          },
        },
        (err, updatedCart) => {
          if (err) {
            console.log(err);
          }
        }
      );
    } else {
      if (!foundCart) {
        const newCart = new Carts({
          _id: req.session.cartId,
          items: [],
        });
        newCart.save();
      }
    }
  });

  res.send('product added to cart!!');
});

module.exports = router;

example of the array

 items: [
    { quantity: 1, _id: '6356ffb3ece7e49784bfbd5d' },
    { quantity: 1, _id: '6356ffb3ece7e49784bfbd5d' },
    { quantity: 1, _id: '6356ff91ece7e49784bfbd5a' },
    { quantity: 1, _id: '6356ff75ece7e49784bfbd57' },
    { quantity: 1, _id: '63570003ece7e49784bfbd69' },
    { quantity: 1, _id: '63570003ece7e49784bfbd69' },
]

Share

0

I’m not really sure about this but it looks like you might have an issue with async methods.

This method does not have neither an await nor a callback function, so the proccess will continue (and maybe finish) without waiting for it to finish.

    Carts.updateOne(
       {_id: req.body.productId },
       { $inc: {  quantity: 1 } }
    );

https://mongoosejs.com/docs/api.html#query_Query-updateOne (If you look at the example there, it’s being awaited)

On the other hand, you should get this block:

  Carts.updateOne(
    { _id: foundCart._id },
    {
      $push: {
        items: {
          _id: req.body.productId,
          quantity: 1,
        },
      },
    },
    (err, updatedCart) => {
      if (err) {
        console.log(err);
      }
    }
  );

Inside of an else block. So if you find an existing item with the id you try to update it, but only if you don’t find it you’ll push a new item into the cart.

It should look something like this:

  if (check.includes(req.body.productId)) {
    console.log('MATCH FOUND');
    Carts.updateOne(
       {_id: req.body.productId },
       { $inc: {  quantity: 1 } }
    );
  } else {
    Carts.updateOne(
      { _id: foundCart._id },
      {
        $push: {
          items: {
            _id: req.body.productId,
            quantity: 1,
          },
        },
      },
      (err, updatedCart) => {
        if (err) {
          console.log(err);
        }
      }
    );
 }

I hope that does the trick! 😀

Share

New contributor

Facundo Gallardo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT