问题描述
所以我将 EJS 与 node 和 mongodb 一起使用。将我的选项的选定值发布到数据库后,我的 put 方法不会更新选项值,而是向我显示先前从发布操作中选择的选项。
例如,我首先选择了陌陌账户选项并发布了它,效果很好。然而,当我将选项更新为银行账户并更新它时,我仍然得到陌陌账户作为我的结果。
// post routes starts here
router.post('/driver/payments',(req,res) => {
let newPayment = {
medium: req.body.medium
}
Payment.create(newPayment)
.then(payment => {
res.redirect('/driver/payments')
})
.catch(err => {
console.log(err)
})
})
// put routes starts here
router.put('/editpayment/:id',res) => {
let searchQuery = { _id: req.params.id }
Payment.updateOne(searchQuery,{
$set: {
medium: req.body.medium
}
})
.then(payment => {
res.redirect('/driver/payments')
})
.catch(err => {
console.log(err)
})
})
This is the POST HTML
<form action="/driver/payments" method="POST">
<select class="form-select" name="medium">
<option selected>Select option</option>
<option value="Momo">Momo Account</option>
<option value="Bank Account">Bank Account</option>
</select>
</form>
This is the PUT HTML
<form action="/editpayment/<%= payment.id%>?_method=PUT" method="POST">
<input type="hidden" name="method" value="PUT">
<select class="form-select" name="medium">
<option selected>Select option</option>
<option value="<%= payment.medium%>">Momo Account</option>
<option value="<%= payment.medium%>">Bank Account</option>
</select>
</form>
excerpts of Schema;
medium: {
type: String,required: true
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)