所以我将 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
}