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:
router.post('/getProducts/addtocart:_id', async(req, res)=> {
var _id = req.params._id;
let productCart= await Products.findById({_id});
......mongosse connection
if(err){
console.log(err)
}else{
console.log("Count:", count)
}
if(count>0){
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(err)
}
else{
console.log("What is ID for res", productCart._id)
res.send((productCart))
}
}
)
}
}else{
.........
Response with new _id
calling function:
handleAddToCart(){
this.Valuesize;
this.cartService.addProductToCart(this.productItem,
this.productItem._id, this.Valuesize).subscribe(()=> {
this.form.reset();
})
this.handleUpdateCart()//call to update cart
}
handleUpdateCart(){
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.