MongoDB/Mongoose Challenge #8 [SOLVED]

MongoDB/Mongoose Challenge #8 [SOLVED]
0
#1

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.

0 Likes

#2

Could someone please guide me to get the solution?

0 Likes

#3

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

1 Like

#4

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)));
});
};

0 Likes

#5

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);
      }
  });
0 Likes

#6

Thank you.
That yields the same error, though.

0 Likes

#7

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

#8

Thank you.
How about now-

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

#10

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 .

0 Likes

#11

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

#12

Of course:

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

#13

How about package.json

1 Like

#14
{
	"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"
}
0 Likes

#15

Why did you include bluebird?

1 Like

#16

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

0 Likes

#17

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

#18

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

1 Like

#19

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

0 Likes

#20

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.

1 Like

#21

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

1 Like