Beta - Mongoose Challenges - Perform Classic Updates by Running Find, Edit, then Save

Beta - Mongoose Challenges - Perform Classic Updates by Running Find, Edit, then Save
0.0 0

#1

Hey guys,

Wondering if someone can help me with one of the Mongoose challenges on the beta site. It’s this one: https://beta.freecodecamp.org/en/challenges/mongodb-and-mongoose/perform-classic-updates-by-running-find-edit-then-save

Here’s my code:

let findEditThenSave = function(personId, done) {
  let foodToAdd = 'hamburger';

  Person.findById({ _id: personId }).update({favoriteFoods: foodToAdd}, (err, data) => {
    Person.save();
    if (err) { done(err) }
    else { done(null, data) }
  });
};

The result on the beta site is: “item.name is not what expected”

Have played around with a few different approaches and nothing’s working. Can someone help me out?


#2

Got help in the chat and got it to work with the following code:


let findEditThenSave = function(personId, done) {
  let foodToAdd = 'hamburger';
  
  Person.findById(personId, (err, data) => {
    if (err) { done(err) }
    console.log(data);
    data.favoriteFoods.push(foodToAdd);
    data.save((err, data) => {
    if (err) { done(err) }
    else { done(null, data) }
  });
    // done(null, data);
  });
  
  // done(null/*, data*/);
};

#3

this is my code , the test is completed but not able to submit

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