I think it’s because you are calling this.favoriteFoods.
this has no meaning inside the callback. The result of the search is returned to you as the variable you named data
So…
// not this
Person.findById(personId, function(err, data) {
this.favoriteFoods.push(foodToAdd).save();
// but this
Person.findById(personId, function(err, data) {
data.favoriteFoods.push(foodToAdd).save();
Thanks for the reply. I’ve changed “this” into “data”, which didn’t work. Also separated the “save” as it gave an error in the previous example. So, my code is now:
var findEditThenSave = function(personId, done) {
var foodToAdd = 'hamburger';
Person.findById(personId, function(err, data) {
data.favoriteFoods.push(foodToAdd);
data.save();
if (err) {
return done(err);
}
else {
done(null, data);
}
});
};
Which results in the following error messages:
// running test
The item should be previously edited
// tests completed
and
Find-edit-update an item should succeed
and the Activity Log in Glitch.com says something about an Unhandled Promise Rejection Warning
I won’t be much more help without seeing more of your codebase. You can post it here or link me to a git repo and I’ll take a look if you want. A repo would be better because I can then run my own tests instead of relying on what I think is happening.
Ok, so the .save() method also returns a promise. So you have to handle that as well. This is why you were getting the Unhandled Promise Rejection Warning.
I added a console.log to the save method and extracted the message from the error object. You will now see a specific reason why it’s failing.
var findEditThenSave = function(personId, done) {
var foodToAdd = 'hamburger';
Person.findById(personId, function(err, doc){
if (err) throw err;
console.log('Beginning of findById()' + doc);
doc.favoriteFoods.push(foodToAdd);
doc.markModified('favoriteFoods');
console.log('after push() and markModified()' + doc);
doc.save(function(err){
if (err) {
console.log("there's an error");
done(err);
}
});
done(null, doc);
});
};
which results in this log on Glitch:
POST
Beginning of findById(){ name: ‘Poldo’,
age: 40,
favoriteFoods: [ ‘spaghetti’ ],
_id: 5b902fad24c4c56ef3d5741c,
__v: 0 }
after push() and markModified(){ name: ‘Poldo’,
age: 40,
favoriteFoods: [ ‘spaghetti’, ‘hamburger’ ],
_id: 5b902fad24c4c56ef3d5741c,
__v: 0 } there’s an error
Error: Can’t set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11) …
And the FCC test says:
The item should be previously edited
and
Find-edit-update an item should succeed
which is confusing, because it was indeed edited as the console.logs show.
Figured it out thanks to this fcc forum post. The only problem I had was that I was using two done(null, data). I should only write that once, within save(), and not within find(). Otherwise it sends the results of the find statement before saving.
Thank you so much, if I didn’t find your answer about only using .done() once I think I would have been stuck forever. The “lessons” in the fcc challenges don’t make this clear at all.