I'm having trouble performing updates Using model.findOneAndUpdate()

I'm having trouble performing updates Using model.findOneAndUpdate()
0

#1

I’m having trouble passing the test. I keep getting:
// running tests
item.favoriteFoods is not what expected
// tests completed

Link to FCC challenge: https://learn.freecodecamp.org/apis-and-microservices/mongodb-and-mongoose/perform-new-updates-on-a-document-using-model-findoneandupdate/

Here’s my code:

var ageToSet = 20;

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

#2

You don’t need to return done in here. Try removing ‘return’ keyword.


#3

I tried it but i’m still getting the same error


#4

Also try removing done from here.

I also hope you have

var findAndUpdate = function(personName, done) {

before your var ageToSet = 20;


#5

yes I removed return from both done(null, data) and done(err), and I have var findAndUpdate = function(personName, done). I don’t know what i’m doing wrong


#6

I am not so sure why yours is not passing.

Here is my code that passes but doesn’t look much different from yours. Maybe you can spot a difference.

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

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

#7

I’m just going to move on to the next challenge and skip this one. Thank you :slight_smile:


#8

Hi! I have just been having the same problem, i added some default values to this schema so when the check code is run, the favouriteFoods is “unknown” and this is not in my default values at schema.

hope this helps so you can finish all challenges!

regards