Error updating Mongoose document

Recently, I worked with node and mongoose , and I liked it until I had to update the model.

That's what I'm doing:

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

So, I thought it would be just like saving a new publication, but it complains with an error:

err: 'Mod on _id not allowed'

I tried removing post._id before passing it to my update method, but it didn’t work, and I couldn’t find good examples of how to do this, except for those that look odd bit, where you first find Post on _id , then update each key manually and save the message again ...

Any suggestions?

+2
source share
1 answer

You were on the right track by deleting post._id before passing it to update . Assuming post is a simple 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) 
+2
source

Source: https://habr.com/ru/post/953671/


All Articles