MongoDB/Mongoose Challenge #8 [SOLVED]

For this challenge, neither of these codes are passing (which are virtually the same)-

#1

var findEditThenSave = function(personId, done) {   
  var foodToAdd = 'hamburger';  
  Person.findById(personId, (err, data) => {
    if (err) { done(err) }    
    data.favoriteFoods.push(foodToAdd);
    data.save((err, data) => {
      if (err) { done(err) }
      else { done(null, data) }
    });    
  });
};

#2

var findEditThenSave = function(personId, done) {   
  var foodToAdd = 'hamburger';    
  Person.findById(personId, function(err, data) {
    if (err) {
      done(err);
    }
    data.favoriteFoods.push(foodToAdd);
    data.save((err, data) => (err ? done(err) : done(null, data)));
  });
};

It yields Unknown modifier: $pushAll
I can’t figure it out.
The only helpful post I found is not helping me neither.

Could someone please guide me to get the solution?

Just to see what happens, rather than using the push() method, try findByIdAndUpdate()
https://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate

1 Like

Hello, the problem is that you don’t need to use the done(err) callback twice, you only need to use it at the save function.
try this:

var findEditThenSave = function(personId, done) {
var foodToAdd = ‘hamburger’;
Person.findById(personId, function(err, data) {
data.favoriteFoods.push(foodToAdd);
data.save((err, data) => (err ? done(err) : done(null, data)));
});
};

2 Likes

Thank you. Should the code look like this?

Person.findByIdAndUpdate({_id: personId}, {favoriteFoods: foodToAdd}, function(err, updatedPerson) {
    if(err) {
      console.log(err);
    } else {        
        console.log(updatedPerson);
      }
  });

Thank you.
That yields the same error, though.

Not quite. Here’s the general pattern from the Mongoose docs:

Model.findByIdAndUpdate(id, { name: 'jason bourne' },  callback)

You can just pass in the id as it is. Looks like personId in your case.

Update: Sorry, I don’t know what I was thinking; this won’t work because favoriteFoods is an array. Give me some time and I’ll see if I can get down to why you’re getting that error.

1 Like

Thank you.
How about now-

Person.findByIdAndUpdate(personId, {favoriteFoods: foodToAdd}, function(err, data) {
    if(err) {
      console.log(err);
    } else {        
        done(null, data);
      }
  });

No. The FCC log yields item.favoriteFoods is not what expected
This is data-

{ _id: 5c6acdf061c3560bec4f6009,

12:23 PM

name: 'Poldo',

12:23 PM

age: 40,

12:23 PM

__v: 0,

12:23 PM

favoriteFoods: [ 'spaghetti' ] }

It doesn’t update the field as it seems the app requieres to push the data .

Yeah, I just updated one of my previous answers to mention that. I’ll see if I can figure out why your code above isn’t working. But, first, can you show me the code for your person schema?

1 Like

Of course:

var personSchema = new Schema({  
  name: {type: String, required: true},
  age: {type: Number},
  favoriteFoods : [String] 
});

How about package.json

1 Like
{
	"name": "fcc-mongo-mongoose-challenges",
	"version": "0.0.1",
	"description": "A boilerplate project",
	"main": "server.js",
	"scripts": {
      "start": "node server.js"
	},
	"dependencies": {
	  "express": "^4.12.4",
      "body-parser": "^1.15.2",
      "mongoose": "4.13.17",
      "bluebird": "3",
      "mongodb": "~3.1.13"
	},
	"engines": {
		"node": "10"
	},
	"repository": {
		"type": "git",
		"url": "https://hyperdev.com/#!/project/welcome-project"
	},
	"keywords": [
		"node",
		"hyperdev",
		"express"
	],
	"license": "MIT"
}

Why did you include bluebird?

1 Like

Got an earlier error regarding handling the promises trying to pass other challenges.

Try this… add usePushEach: true to your schema.

var personSchema = new Schema({  
  name: {type: String, required: true},
  age: {type: Number},
  favoriteFoods : [String] 
}, {
  usePushEach : true
});
1 Like

If this doesn’t work, try changing your mongoose dependency to the latest version, 5 or higher.

1 Like

That did it.
Thank you very much for your time and help.

Awesome!
If you’re interested in why you were getting that error, check out this thread at GitHub:

It seems like usePushEach is standard in Mongoose version 5 and up.

2 Likes

Of course I’m interested. Thank you for that article.

1 Like