MongoDB and Mongoose - Perform New Updates on a Document Using model.findOneAndUpdate()

MongoDB and Mongoose - Perform New Updates on a Document Using model.findOneAndUpdate()
0

#1

It seems there is a bug in the testing. It updates and returns in updated record in the console. However, it does not pass teh test. can anyone tell me what is wrong please?


var findAndUpdate = function(personName, done) {
  var ageToSet = 20;
  
  //console.log("person name  : ", personName);
  Person.findOneAndUpdate(
    {"name": personName},
    {$set: {"age":ageToSet}},{returnNewDocument : true}, 
    function(err, doc){
                    if(err){
                        console.log("Something wrong when updating record!");
                    }
                    console.log(doc);
})};

The console returns

POST

person name is :  Dorian Gray

{ favoriteFoods: [ 'unknown' ],

  _id: 5b370e3cb62c7d06e2553240,

  name: 'Dorian Gray',

  age: 20,


Thank you in advance.


#2

I got it. The issue was with error function. It does not follow exercise format. Just a note: done is an error handler function!


#3

Can you please show the solution?


#4

Here is a working piece of code guys :

var findAndUpdate = function(personName, done) {
  var ageToSet = 20;

  Person.findOneAndUpdate(
    {name: personName},
    {$set: {age: ageToSet}},
    {new: true},
    (err, data) => {
      if (err) return done(err, data);
      return done(null, data);
    }
  );
};