2

Been working with node and mongoose lately and I enjoyed it until I had to update a model.

Here is what I'm doing:

module.exports.update = (post, cb) ->
  Post.update _id: post._id, post, (err, data) ->
    cb(err, data)

So I thought it'll be a easy as saving a new post but it's complaining with error:

err: 'Mod on _id not allowed'

I tried to delete post._id before passing it to my update method, but it didn't work and I couldn't find any good examples on how to do it except one that looks a bit odd where first you find Post by _id, then update each key manually and save Post back again...

Any suggestions?

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
daniel
  • 1,205
  • 1
  • 10
  • 15

1 Answers1

2

You were on the right track with deleting post._id before passing it to update. Assuming post is a plain JS object, this should work:

module.exports.update = (post, cb) ->
  id = post._id
  delete post._id
  Post.update _id: id, post, (err, data) ->
    cb(err, data)
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471