Stuck on MongoDB and Mongoose - Perform Classic Updates by Running Find, Edit, then Save

Tell us what’s happening:
Don’t know what to do.

Your code so far

var findEditThenSave = function(personId, done) {
  var foodToAdd = "hamburger";
  Person.findById(personId, function(err, data) {
    if (err) return console.log(err);
      Person.favoriteFoods.push("hamburger")
      Person.save()
    done(null, data);
  });
};

https://lily-hexagon.glitch.me

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: undefined

Link to the challenge:
https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/perform-classic-updates-by-running-find-edit-then-save

Hello Bilal.

From what I know, findById does not take a callback function as a second parameter.

Hope this helps.

Model.findById() takes a callback function:

https://mongoosejs.com/docs/api.html#model_Model.findById

Hey @Bilalm354, A little late but I was also stuck on this for some time. From the little that I understand, you need to update and save the object passed into the callback by findById. My code which passed:

var findEditThenSave = function(personId, done) {
  var foodToAdd = 'hamburger';
  var person = Person.findById(personId, function(err, person){
    if(err) return console.log(err);
    person.favoriteFoods.push(foodToAdd);
    person.save(function(err, data){
      if(err) console.log(err);
      done(null, data)
    });
  })
}
2 Likes