I have a need for an unusual update, the reason I say it is unusual is
because its all server side.
Let me explain, What I am doing to handle a duplicate if the user wants to order more than one of same item in cart(MongoDB does not except duplicates). I check if the id exist by doing a count, if exist I create a generated id using Object() with no parameters. Every thing work fine until I try to do an update because I have to add a size currently in database param is empty. The updateOne does not work.
When there is no id exist I’m able to insert it return to calling function which calls update it works as expected. I will have to modify this later If I do it this way with for update on new ObjectId it won’t recognize it will update the in the cart on original Id item. But I can’t get update to work in this scenario. I hope its possible unless I will have to go back to drawing board. If someone can point me in the right direction it would be great or tell me what I’m doing wrong. It would be greatly appreciated.
Code snippet below;
router.post('/getProducts/addtocart:_id/:Valuesize', async(req, res)
if(count>0){
console.log("Does ti get to count")
console.log("Product exist")
productCart._id = ({"_id" : ObjectId()})
//I am on a mongoose connection
console.log("what is new productCart ID", productCart._id)
db.collection('carts').insertOne(productCart, function(err,
result){
if(err) {
console.log(err)
}
else{
res.send(result).status(200);
//db.close();
}
console.log("what is new id", productCart._id)
//Here I tried with the model as well but with same result
db.collection('carts').updateOne( {"_id":
ObjectId(productCart._id)},
{ $set: {"size": size}, function(err, result){
if(err){
console.log(err);
}
else{
res.send(result).status(200);
}
}
})
console.log("What doc inserted", "" + result)
console.log("1 document inserted");
}
)
console.log("what is new id", productCart._id)//ensure its new id
db.collection("carts").updateOne( {"_id":
ObjectId(productCart._id)},
{ $set: {"size": size}
//update is where I'm getting stuck no errors, nothing happens
})
}else{
//here I do regular insert if flase