我正在创建一个执行CRUD操作的程序,并得到了一个错误:
强制转换为ObjectId失败的值\“:5e1360c5edb2922570a2611\”在路径\"_id\“模型\”,
这是我的代码:
// here is the route of edit path
app.get("/edit/:ids", (req, res) => {
colt.findById(req.params.ids, (err, coltz) => {
if (err) {
res.redirect("/");
} else {
res.render("showedit", { colt: coltz });
}
});
});
// here is the update route
app.put("/edit/:ids", (req, res) => {
colt.findByIdAndUpdate(req.params.ids, req.body.colts, err => {
if (err) {
res.send(err);
} else {
res.redirect("/show");
}
});
});下面是编辑ejs文件的节目:
<h1>HELLO HERE YOU CAN UPDATE</h1>
<form action="/edit/:<%=colt._id%>?_method=PUT" method="POST">
<input type="text" placeholder="name here" name="colts[name]" value="<%= colt.name %>">
<input type="text" placeholder="description here" name="colts[description]" value="<%= colt.description %>">
<input type="text" placeholder="img url here" name="colts[url]" value="<%= colt.url %>">
<input type="submit">
</form>这是我的猫鼬模式:
var coltSchema = new mongoose.Schema({
name: String,
description: String,
url: String
});
let colt = mongoose.model("colt", coltSchema);发布于 2020-01-07 06:57:28
使用url params时,不必在链接中写:
<form action="/edit/:<%=colt._id%>?_method=PUT" method="POST"> // remove : from this line因此,您的路径在is中获得的值是:5e1360c5edb2922570aa2611,而不是仅5e1360c5edb2922570aa2611。
从表单动作的url中移除:您可以开始了
https://stackoverflow.com/questions/59623343
复制相似问题