MongoDB and Mongoose - Perform Classic Updates by Running Find, Edit, then Save

I seem to have issues with this lesson, and im not sure what i’m doing wrong. I feel as if the documentation isn’t explanatory enough when looking at the solution. Unless I’m mistaken, I’ve already declared the type of “favoriteFoods” as an Array but am confused on what’s going on with my code when I attempt to push the item into the favoriteFoods array. Could someone please explain it a little better than the lesson? If I need to use the document.markModified(‘edited-field’), how can I incorporate this successfully into my code? I just can’t seem to understand the information from documentations.

Your project link(s)

solution: boilerplate-mongomongoose - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35

Challenge: MongoDB and Mongoose - Perform Classic Updates by Running Find, Edit, then Save

Link to the challenge:

I can’t test it now, but I THINK your problem is here:

Person.favoriteFoods.push(foodToAdd); and here:

Person.save((err, updatedPerson) => {}

Person is the model that allows you to talk to the Person collection in your database, but person is the result from your search, which you define with the arrow function argument below:

Person.findById(personId, (err, person) => {}

You would then need to save the person instance of the object rather than the Person model.

I agree with you that this segment was not very well explained. I had to do a lot of reading and researching other sources for this one. I especially had a hard time understand the callback functions, and I have switched to more often using async/await instead.

I recommend the net ninja’s async tutorials on youtube, which helped me. Good luck!

2 Likes

Ahh yes, that’s entirely correct. The apparent problem is that I capitalized the letter P in person instead of leaving it lower cased which lead to some confusion in the rest of the coding. I assume that’s why these things should be named differently. I went ahead and changed the variable names to corelate between the Person collection, where I’m pushing the data into / returned data from that collection, which I named (personFound), and returning the updated collection reference, (updatedPerson). Hopefully I explained that correctly lol. Thanks again for your time, it helped me realize what I was doing incorrectly!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.