英文:
I am New to nodejs and trying to update a category in categories table but it is not working
问题
我正在尝试使用updateOne方法来更新数据,但我无法调试它,不知道为什么它不起作用?
router.post('/edit-category/:slug', async (req, res) => {
try {
const updatedPost = await Category.updateOne(
{ _id: req.body.id },
{
$set: { title: req.body.title, slug: req.body.slug }
}
);
res.send(updatedPost);
} catch (error) {
console.log({ message: error });
}
});
英文:
> I am trying to update a data using updateOne method but i am not able to debug it why it is not working ?
router.post('/edit-category/:slug', async (req,res) =>{
// res.send(req.body.id);
try{
const updatedPost = await Category.updateOne(
{ _id: req.body.id},
{
$set: { title: req.body.title },
$set: { slug: req.body.slug }
}
);
// updatedPost.update((error) => {if(error){console.log("hiiiiiiiii"+error)}});
res.send(updatedPost);
// console.log(updatedPost);
}catch(error){
console.log({message:error})
}
});
答案1
得分: 0
-
在数据库中检查文档是否存在于数据库中,其中
req.body.id
作为_id
。 -
尝试以下代码:
const ObjectId = require('mongodb').ObjectID; const updatedPost = await Category.updateOne({ _id: ObjectId(req.body.id)})
英文:
Two possibilities:
-
Check in DB whether the document is there in DB with
req.body.id
as_id
-
Try for the below code:
const ObjectId = require('mongodb').ObjectID;
const updatedPost = await Category.updateOne({ _id: ObjectId (req.body.id)}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论