Tell us what’s happening:
Trying to solve the challenge.
I am able to succeed when I am trying it with findById and then retrieving the document and pushing the value into the array. But when I want to try using the findByIdAndUpdate, I am not able to.
const foodToAdd = "hamburger";
Person.findByIdAndUpdate(
personId,
{ $push: { favoriteFoods: foodToAdd } },
(err, data) => {
console.log(data);
if (err) console.error(err);
done(null, data);
}
);
The schema and model used is:
let personSchema = new Schema({
name: {
type: String,
required: true,
},
age: Number,
favoriteFoods: [String],
});
let Person = mongoose.model("Person", personSchema);
I am not sure why the value is not being pushed into the array.
Your project link(s)
solution: Glitch :・゚✧
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39
Challenge: Perform Classic Updates by Running Find, Edit, then Save
Link to the challenge: