Perform Classic Updates by Running Find, Edit, then Save
Relevant Links
Hints
Hint 1
Try to use .findById()
method to find person by id .
Hint 2
Use Array.push()
method inside callback function to add given string to favoriteFoods array .
Hint 3
Don’t forget the save
updated person with a callback function
Solutions
Solution 1 (Click to Show/Hide)
const findEditThenSave = (personId, done) => {
const foodToAdd = 'hamburger';
// .findById() method to find a person by _id with the parameter personId as search key.
Person.findById(personId, (err, person) => {
if(err) return console.log(err);
// Array.push() method to add "hamburger" to the list of the person's favoriteFoods
person.favoriteFoods.push(foodToAdd);
// and inside the find callback - save() the updated Person.
person.save((err, updatedPerson) => {
if(err) return console.log(err);
done(null, updatedPerson)
})
})
};