MongoDB and Mongoose - Create and Save a Record (SAVE ISSUE)

hello i am encountering the following error after pushing the array ‘favoriteFoods’:

UnhandledPromiseRejectionWarning: VersionError: No matching document found for id “5de95a18c45e5b135b4d0070” version 0 modifiedPaths “favoriteFoods”

this occurs in the save method.

hello,
follow this link https://stackoverflow.com/questions/45223025/mongoose-version-error-no-matching-document-found-for-id same issue as yours resolved.

@OJL use a update method instead? i tried to… but don’t pass in tests

one possible solution I have found on https://github.com/Automattic/mongoose/issues/1844 is to disable versioning or use the skipVersioning to selectively skip a field for versioning if you’re sure you’re not worried about conflicting changes.
by vkarpov15 commented on Oct 19, 2016

If you are using Glitch, can you post a link to your project?

hello, any other suggestions? I’m really stuck in this … :confused:

2 Likes

I had the same issue as you xandeact and couldn’t figure out what was going on - I remixed your proj and used your code and it worked! I saw that you added the done callback in the save function, instead of after the save function. Fixed for me, thanks.

Before:

Person.findById(personId, function(err, p){
    if (err) return console.error(err);
    p.favoriteFoods.push(foodToAdd);
    p.save();
    done(null, p);
  });

After:

Person.findById(personId, function(err, p){
    if (err) return console.error(err);
    p.favoriteFoods.push(foodToAdd);
    p.save(function(err, p){
      if (err) return console.error(err);
      done(null, p);
    });
  });
2 Likes

You saved my life. I was trying many solutions for a few days but none of them worked.

It worked!

var findEditThenSave = function(personId, done) {
  var foodToAdd = "hamburger";

  Person.findById(personId, function(err, personFound) {
    err ? console.error(err) : personFound;
    personFound.favoriteFoods.push(foodToAdd);
    personFound.markModified("edited-field");
    personFound.save((err, personFound) => (err ? console.error(err) : done(null, personFound)));
  });
};