lundi 10 mai 2021

Everytime I push new value in array it does not get update and new value is added on same index

I am creating a cart. For this I first check if the cart is undefined if it is undefined then I create a cart and push items objects in it, else if there is a cart then I check for the item and if found I just increase its quantity, however every time I click add to cart new cart is created and an only new object is added.

Code:

 router.get('/cartItem/:id', (req, res) => {

  if (typeof req.session.cart == "undefined") {


    req.session.cart = []
    req.session.push({
      data: "message"
    })
    console.log("after", req.session)

  } else {
    console.log("increase quantity !!")
  }


})



Aucun commentaire:

Enregistrer un commentaire