Monday, 24 April 2017

Mongoose - Modle.update() updates wrong document - Cast Error

I need some help to clear some things up.

I have a Model:

var Event = new Schema({
   event_code: String
 , segments: [Segment]
});

The creation of new documents work very well like perfect. When it comes to update certain documents I ran into some troubles.

When I do this (code below): = it only updates the first document, even if the id does not match

function edit_event (id, new_name, callback) {
  Event.update(id, {$set:{event_code: new_name}}, function(err, doc) {
    if (err) throw err;
    callback();
  });
}

When I do this (code below): = it gives me an Error (see below)

function edit_event (id, new_name, callback) {
  Event.findByIdAndUpdate(id, {$set:{event_code: new_name}}, function(err, doc) {
    if (err) throw err;
    callback();
  });
}

Error when using findByIdAndUpdate: Cast to ObjectId failed for value ""58fdbde31bff83141b376508"" at path "_id" for model "Event"

Please, i'm desperate :! :/



via Pascal Lamers

No comments:

Post a Comment