Tell us what’s happening:
I’m doing the Perform Classic Updates by Running Find, Edit, then Save project under Back End Development and APIs - MongoDB and Mongoose section.
But I couldn’t push the new value foodToAdd to the favoriteFoods field because it’s undefined. It says “TypeError: Cannot read property ‘push’ of undefined”.
What’s going wrong with my code?? Thank you!
Your code
const personSchema = new mongoose.Schema({
name: String,
age: Number,
favoriteFoods: [String]
});
const Person = mongoose.model("Person", personSchema);
const findLily = async () => {
const lily = new Person({ name: "Lily", age: 5, favoriteFoods: ["Vanilla Cake", "Lollipop"] });
await lily.save();
const found = await Person.find({ name: "Lily" });
console.log(found);
console.log(found.favoriteFoods); // undefined
console.log(typeof (found.favoriteFoods)); // undefined
}
findLily();
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
Challenge: Perform Classic Updates by Running Find, Edit, then Save
Link to the challenge: