我是新手nodejs,尝试更新categories表中的一个类别,但不起作用。

huangapple go评论81阅读模式
英文:

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

  1. 在数据库中检查文档是否存在于数据库中,其中 req.body.id 作为 _id

  2. 尝试以下代码:

    const ObjectId = require('mongodb').ObjectID;
    const updatedPost = await Category.updateOne({ _id: ObjectId(req.body.id)})
    
英文:

Two possibilities:

  1. Check in DB whether the document is there in DB with req.body.id as _id

  2. Try for the below code:

    const ObjectId = require('mongodb').ObjectID;
    const updatedPost = await Category.updateOne({ _id: ObjectId (req.body.id)}

huangapple
  • 本文由 发表于 2020年1月6日 18:07:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定