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

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);
 }
);
};```

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

1 Like

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

Also try removing done from here.

I also hope you have

var findAndUpdate = function(personName, done) {

before your var ageToSet = 20;

1 Like

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

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);
  })
};
2 Likes

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

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

1 Like

This works for me

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);
  });

};

Try this

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);
  });

};
1 Like

Please can you share what you did exactly?

I have the same problem too.

I just solved this.

Comment out the array you created in the “Create many People with Model.create()”


/** 4) Create many People with `Model.create()` */

// Sometimes you need to create many Instances of your Models,
// e.g. when seeding a database with initial data. `Model.create()`
// takes an array of objects like [{name: 'John', ...}, {...}, ...],
// as the 1st argument, and saves them all in the db.
// Create many people using `Model.create()`, using the function argument
// 'arrayOfPeople'.
/*
var arrayOfPeople = [
  {
    name: 'Mary Doe',
    age: 2,
    favoriteFoods: ["Egg", "Yammy"]
  },
  {
    name: 'Kovani Roe',
    age: 15,
    favoriteFoods: ["Rice", "Yam"]
  },
  {
    name: 'Seth Jane',
    age: 25,
    favoriteFoods: ["Fish", "Yam"]
  }
];
*/

var createManyPeople = function(arrayOfPeople, done) {
    Person.create(arrayOfPeople, (err, data) => {
      err ? done(err) : done(null, data);
    });    
};

I am also not able to pass with solution not even with the ones other have shared. Can someone have a look?