I started working with Mongoose with Express.js. I am trying to insert a record into the database and update it if it exists.
I am using findOneAndUpdate
for this purpose. But it does not work. Data is not inserted and error
and result
in the callback are undefined.
Here is my code:
Products.findOneAndUpdate({"username": data.username, "uniqueid": newproduct.uniqueid}, newproduct, {upsert: true}, new function(err, result) {
//It returns err = undefined result = undefined
if(err) {
callback({"statuscode" : 203, "msgkey" : "add.product.failure","data": {"message": err}});
}
callback({"statuscode" : 200, "msgkey" : "add.product.success", "data" : newproduct});
What could be going wrong here?