简体   繁体   English

使用 express node.js 更新数据时出现错误

[英]i am getting an error while i am updating my data using express node.js

i am trying to update my category name but when i click on the submit button it gives me an error however i tested my code using postman and it actually updated but in that case i have to pass the product's id manually.我正在尝试更新我的类别名称,但是当我点击提交按钮时,它给了我一个错误但是我使用邮递员测试了我的代码并且它实际上更新了但在这种情况下我必须手动传递产品的 ID。

the callback function of the update category:更新类的回调函数:

    exports.updateCategory = (req, res, next) => {

    console.log('GET update CATEGORY /update-category');

    if (!req.body) {
        return res
            .status(400)
            .send({ message: "Data to update can not be empty" })
    }

    const id = req.params.id; //req.body.id >> body means in the html/ejs file the name should be id

    categorySchema.findByIdAndUpdate(id, req.body, { userFindAndModify: false })
        .then(data => {
            if (!data) {
                res.status(404).send({ message: `Cannot Update user with ${id}` })
            }
            //updating the data
            else {
                res.render('category/edit_category.ejs', {
                    category: data //variable that i am going to use is category in the ejs
                })
            }

        })

    .catch(err => {
        res.status(500).send({ message: "Error update user information" })
    })


}

the update form >>更新表格>>

    <form action='<%=`/halalMunchies/update-category/${category._id}`%>' method="post">
.
.
.
<label class="control-label" for="categoryName"> Category Name </label>
        <input type="hidden" name="id" value="" id="id">
        <input id="categoryName" class="form-control" value="<%= category.categoryName%>" name="categoryName" required autofocus="autofocus" />

the router.js路由器.js

    //PUT UPDATE CATEGORY

router.put('/update-category/:id', categoriesController.updateCategory);

router.get('/update-category/:id', categoriesController.updateCategory);

the error message on page is :页面上的错误消息是:

Cannot POST /halalMunchies/update-category/618632e3a5ad00fa5368ad5c

As the error message tells you, you did not provide a post endpoint in your router.正如错误消息告诉您的那样,您没有在路由器中提供post端点。 Since you're using the post method in your form, you need to add the following in your router.js:由于您在表单中使用post方法,因此需要在 router.js 中添加以下内容:

router.post('/update-category/:id', categoriesController.updateCategory);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将我的项目上传到 Heroku 时出现此错误。 我正在使用 MapBox 和 node.js - I am getting this error while uploading my project to Heroku. I am using MapBox and node.js 在Mac上通过Node.js创建Phonegap项目时出现错误 - I am getting an error while creating a Project of Phonegap through Node.js in my mac 运行node.js应用程序时出现错误(也使用Rabbit) - I am getting an error when i run my node.js app ( also using rabbit ) 为什么我的本地node.js / express服务器的响应中没有得到JSON对象? - Why am I not getting a JSON object in the response from my local node.js/express server? 使用 monogoose 将 object 推送到节点 js 中的数组属性时出现错误 - I am getting an error while i am pushing an object to the array property in node js using monogoose 运行node.js脚本时出现以下错误 - I am getting following errors while running my node.js script 我的服务器已启动,但是当我尝试连接它时,出现错误[Node.js] - My Server is up but when I try to connect to it I am getting an error [Node.js] 为什么我从node.js收到此错误? - Why am I getting this error from node.js? 我收到此错误: Block is not a constructor in node.js - I am getting this error: Block is not a constructor in node.js 在Express JS中将HTML转换为Pug时出现错误 - I am getting error while converting html to pug in express js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM