KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a request which body contains data and _id What is the better way to implement code that will update if record with _id exists and will create one is there is no one? My code: var obj = req.body; Model.findById(obj._id, function(err, data){ if (!data){ var model = new Model(obj) model.save(function(err, data){ res.send({method: 'create', error: err, data: data}) }) } else { Model.findByIdAndUpdate(obj._id, obj, function(){ res.send({method: 'update', error: err, data: data}) }) } I'm just thinking maybe there is beter way of doing this.
Tags (comma-separated)
Save Edits
Cancel