我正在嘗試更新我的類別名稱,但是當我點擊提交按鈕時,它給了我一個錯誤但是我使用郵遞員測驗了我的代碼并且它實際上更新了但在這種情況下我必須手動傳遞產品的 ID。
更新類的回呼函式:
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" })
})
}
更新表格>>
<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" />
路由器.js
//PUT UPDATE CATEGORY
router.put('/update-category/:id', categoriesController.updateCategory);
router.get('/update-category/:id', categoriesController.updateCategory);
頁面上的錯誤訊息是:
Cannot POST /halalMunchies/update-category/618632e3a5ad00fa5368ad5c
uj5u.com熱心網友回復:
正如錯誤訊息告訴您的那樣,您沒有post在路由器中提供端點。由于您post在表單中使用該方法,因此您需要在 router.js 中添加以下內容:
router.post('/update-category/:id', categoriesController.updateCategory);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/351411.html
上一篇:避免回應機器人
