MongoDB and Mongoose - Perform New Updates on a Document Using model.findOneAndUpdate()

It seems there is a bug in the testing. It updates and returns in updated record in the console. However, it does not pass teh test. can anyone tell me what is wrong please?


var findAndUpdate = function(personName, done) {
  var ageToSet = 20;
  
  //console.log("person name  : ", personName);
  Person.findOneAndUpdate(
    {"name": personName},
    {$set: {"age":ageToSet}},{returnNewDocument : true}, 
    function(err, doc){
                    if(err){
                        console.log("Something wrong when updating record!");
                    }
                    console.log(doc);
})};

The console returns

POST

person name is :  Dorian Gray

{ favoriteFoods: [ 'unknown' ],

  _id: 5b370e3cb62c7d06e2553240,

  name: 'Dorian Gray',

  age: 20,


Thank you in advance.

I got it. The issue was with error function. It does not follow exercise format. Just a note: done is an error handler function!

1 Like

Can you please show the solution?

1 Like

Here is a working piece of code guys :

var findAndUpdate = function(personName, done) {
  var ageToSet = 20;

  Person.findOneAndUpdate(
    {name: personName},
    {$set: {age: ageToSet}},
    {new: true},
    (err, data) => {
      if (err) return done(err, data);
      return done(null, data);
    }
  );
};
14 Likes

This also passes the test:

var findAndUpdate = function(personName, done) {
  var ageToSet = 20;

  Person.findOneAndUpdate(
      {name: personName}, 
      {age: ageToSet}, 
      {new: true}, 
      (err, data) => {
      if (err) {
         done(err); 
      }
      done(null, data);
    }
  )
};

I removed the $set from line 6 and it seems to still work. And I removed the return statements on lines 10 and 12 (just to match how my other answers look). Also it no longer returns data if there’s an error on line 10.

10 Likes

Wrong!
Modify the app.js file as follow:
//APP CONFIG
mongoose.connect(“mongodb://localhost:27017/dbname”, { useNewUrlParser: true });
mongoose.set(‘useFindAndModify’, false);

In order to get rid of a warning, include the key useFindAndModify in the options-document sent to findOneAndUpdate like this (presumable this is equivalent to what @codedbycarlos wrote above.

var findAndUpdate = function(personName, done) {
  var ageToSet = 20;
  console.log("findAndUpdate, trying:", personName);
  Person.findOneAndUpdate(
    { name: personName },
    { age: ageToSet },
    { new: true, useFindAndModify: false },
    (err, d) => {
      if (err) return done(err);
      done(null, d);
    }
  );
};

Thank you for your reply here! I find that these challenges are not well written. I’m really struggling with these Mongoose challenges, but it’s not a useful struggle. I’m struggling because I don’t have enough direction.

Here are the official docus for findOneAndUpdate

I tried doesn’t work

Here this is solution for this challenge.

var findAndUpdate = function(personName, done) {
var ageToSet = 20;
Person.findOneAndUpdate(
{ name: personName },
{ age: 20 },
{ new: true },
function(err, data) {
if (err) {
done(err);
} else {
done(null, data);
}
}
);
};

I try this code in another new project cannot work but,I don’t know what I changed but , this project work.This is the URL. https://glitch.com/~fcc-mongo-and-mongoose-

This code is working for me!

ar findAndUpdate = function(personName, done) {
  var ageToSet = 20;

  Person.findOneAndUpdate(
      {name: personName}, 
      {age: ageToSet}, 
      {new: true}, 
      (err, data) => {
      if (err) {
         done(err); 
      }
      done(null, data);
    }
  )
};