Skip to content

Instantly share code, notes, and snippets.

@mrhm-dev
Created January 7, 2018 03:19
Show Gist options
  • Save mrhm-dev/10db56b6d73b7b65a530dae6e6a3dd6e to your computer and use it in GitHub Desktop.
Save mrhm-dev/10db56b6d73b7b65a530dae6e6a3dd6e to your computer and use it in GitHub Desktop.
Using a different approach to patch method in this code. value should be pass with request as below [ {obj}, {obj} ]
router.put('/:newsId', (req, res, next) => {
const _id = req.params.newsId;
const updateOptions = {};
for(let ops of req.body) {
updateOptions[ops.propName] = ops.value;
}
// Called Update Method from News Model
News.update({_id}, {$set: updateOptions})
.exec()
.then(result => {
res.status(200).json({
message: 'News Updated',
request: {
type: 'GET',
url: `${URL}/${_id}`
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment