MongoDB Challenge #8: Perform Classic Updates - Ask for Help

Hi there!

I’m having issues with the code required for this lesson:

It sounds like what’s being asked is to:

  1. Find a person by ID
  2. Add “hamburger” to the array “favoriteFoods” under the Person model.
  3. Call inside the callback.

I looked at Mongoose documentation and found the method Model.findByIdAndUpdate. I wrote out some code, and after a lot of editing, was able to get it to not throw any errors.

That said, I’m not passing the automated testing suite. Was hoping somebody could help me work out why. Here is the code I wrote, below:

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }); 

const Schema = mongoose.Schema;

const personSchema = new Schema({
  name: String,
  age: Number,
  favoriteFoods: [String]

const Person = mongoose.model('Person', personSchema);

const arrayOfPeople = [
    {name: 'Jay', age: 56, favoriteFoods: ['Dirt', 'Sand']},
    {name: 'David', age: 48, favoriteFoods: ['Grass', 'Lava']}

const createManyPeople = function(arrayOfPeople, done) {
    Person.create(arrayOfPeople, function (err, people){
      if (err) return console.error(err)
        done(null, people);

// This is the one I need help with.
const findEditThenSave = function(personId, done) {
  const foodToAdd = 'Hamburger';
  Person.findByIdAndUpdate(personId, { $set: { favoriteFoods: foodToAdd }}, { useFindAndModify : false }, function (err, person) {
    if (err) return console.error(err)
      done(null, person);

Do you see any errors in the console? You should if the code you posted above is what you have.

The challenge does not say to use this method. You should be using the save method. Think about how you could use the findById method along with the save method for a solution.

Welcome to freecodecamp forum @par1si! :smiley:

I’m not sure how the validation works for this challenge; i can see an issue in your code though^^

Person.findByIdAndUpdate(personId, { $set: { … }}, { … }, function (err, person) {…})

you use $set operator: this will set favoriteFoods to the value of foodToAdd ( it doesn’t add your item to the array, it replaces it^^)

Thank you all! I’ll try the suggestions above and get back to you. Appreciate the quick response.

The line above should cause an error.

Ah, just a copy/paste error. My fault. Fixed now!

I’m still having some issues here. I updated the bottom block of code to read:

const findEditThenSave = function(personId, done) {
  const foodToAdd = 'hamburger';
  const updatedPerson = Person.findById(personId, function (err, person) {
  if (err) return console.error(err)
  done(null, person);

I looked at Stack Overflow and got a lot of helpful pointers for how to just use vanilla JS to update models in memory, so thanks for the suggestion Randell.

Now I am getting a TypeError thrown saying that .push() can’t read property of undefined. This means that the object being returned to the updatedPerson variable doesn’t include a favoriteFoods property, even though it is defined in the Person model and schema above.

I’m kind of confused, because I’m expecting the object that gets returned by calling Person.findById() to have a .favoriteFoods property.

Can anybody give me a quick hint for where I’m missing something? Thank you so much!

The save method takes a callback function as an argument.