0

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?

ashwin mahajan
  • 1,654
  • 5
  • 27
  • 50
  • You forgot to include `new` in the options. Should be `{upsert: true, new: true }, function(....` – Neil Lunn Aug 08 '17 at 09:04
  • Since `new` defaults to false and returns older object in that case. But it does not return anything. I get `result` and `error` as undefined. – ashwin mahajan Aug 08 '17 at 09:10
  • You also malformed your request. See my correction and also the other usage examples in linked answers and documentation. And plain nothing means your query parameters are incorrect. Check them and make sure they actually match data. – Neil Lunn Aug 08 '17 at 09:12

0 Answers0