Mongoose document update error

大憨熊 提交于 2019-12-24 01:26:01

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/13352735/mongoose-document-update-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!