【发布时间】:2021-10-27 23:11:45
【问题描述】:
我目前正在尝试构建一个以 Node.js 作为后端的应用程序。
在询问了使用什么 API 方法进行更新后,因为我最初使用 post for all,我开始使用 put 和 patch 问题已解决,补丁没有更新我的数据库中的版本字段。
我调用 put 和 patch 的方式有问题还是它们就是这样
router.patch('/update/:id', async (req, res) => {
// get category id
const id = req.params.id;
// check if category is found
try {
const category = await Category.findByIdAndUpdate(id, req.body);
// check if no category is found
if (!category) {
return res.status(400).send('Category not found');
}
// return category
return res.status(200).send(category);
}
catch (err) {
return res.status(400).send(err);
}
});
【问题讨论】:
标签: javascript node.js mongodb mongoose ecmascript-6