skip to Main Content

I have an issue with my request and response. I am handling issues when the customer wants to add more than one of the same item with a different size.
What I first do is check if the Id exist by doing a count on and a if statement. If exist I create a a new object id using ({"_id": ObjectId()}) with no parameters. It all works as expected same object insert but with new id. What I do next is send a different response and it shows the response with the different Id, but when I try to do an update on the response Id it updates the id of the request.

I understand that the response goes back to calling function, but my question is how do I get it to look at the new response Id. I’m stuck, I know I’m missing something. I would be great if some one could point me in the right direction. I would really appreciate your help.

code snippet below:'/getProducts/addtocart:_id',   async(req, res)=> {

   var _id = req.params._id; 

 let productCart= await Products.findById({_id});

 ......mongosse connection

    console.log("Count:", count)

      console.log("Product exist");

      productCart._id = ({"_id" : ObjectId()});

         console.log("what is new productCart ID", productCart._id)
         console.log("now whats in cart with new Id", productCart)

  db.collection('carts').insertOne(productCart, function(err, result){ 
        if(err) {  


          console.log("What is ID for res", productCart._id)




enter image description here

    Response with new _id

enter image description here

calling function:



 this.productItem._id, this.Valuesize).subscribe(()=> {



  this.handleUpdateCart()//call to update cart



   this.cartService.UpdateCart(this.productItem, this.productItem._id, 
   this.size).subscribe(()=> {


As can see from the screen shot I do get the response with the new Id.
I’m not sure if I should add an Id. Any help would be grateful. I’m close but not on the money.



  1. Chosen as BEST ANSWER

    I do appreciate traynor's response to my question, I did try it, but the the way my my logic is in my code this wouldn't work all the time.

    1. Adding the object to the subscribe just using resp. I can get the response id, but the Update was still giving me an issue.

    2. I decided to go another way. I decided insert and update on the API server side. This works better, rather than going back to client and then calling the update function which then calls the update cart service to do update. This new way is less time.

    3. In my products collection I had to do a default size because having an empty parameter never updates it will always be null.

    So in a nutshell the code I shared is the same. The post I added a size in the route path along with the id. After the insertOne I did an updateOne. I do get the results expected which in giving a new id if id exist then doing an update for size chosen. I will do the same(insertOn then updateOne if id does not exist.

  2. You’re sending the new object to the client, but you don’t seem to update old object with the new one.

    So, try updating the object:

     this.productItem._id, this.Valuesize).subscribe((freshObject:any /*or your type: IProductItem */)=> {
        // now replace old with new
        this.productItem = freshObject;
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top