javascript - 对于模型 \"colt\"" 错误的路径 \"_id\" 处的值 \":5e1360c5edb2922570aa2611\" 转换为 ObjectId 失败
问题描述
我正在创建一个程序来执行 CRUD 操作,但出现错误:
对于模型 \"colt\"" 的路径 \"_id\" 处的值 \":5e1360c5edb2922570aa2611\",转换为 ObjectId 失败,
这是我的代码:
// 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);
解决方案
使用 url 参数时,您不必在链接中写:
<form action="/edit/:<%=colt._id%>?_method=PUT" method="POST"> // remove : from this line
因此,您的路线在 ids 中获得的:5e1360c5edb2922570aa2611
值5e1360c5edb2922570aa2611
不仅仅是
从表单操作的 url 中删除 : 就可以了
推荐阅读
- vue.js - 如何在 vuex 发布请求中从状态传递值
- mysql - 需要在 MySQL 中使用 ALTER COLUMN 将列类型更改为 NOT NULL AUTO_INCREAMENT
- abap - 使用 COND 运算符将非空行附加到 itab
- mysql - 将行显示为列
- android - 在 Kotlin 中检查两个对象的某些(不是全部)属性是否相等的惯用方法
- c - fseek 和 Microsoft 的 CRT UNICODE 支持存在问题
- linux - Cronjob 不执行“bundle exec rake -f ...”
- python - 下载文件 Git 存储库
- php - PHPmailer 自定义标头集,但未在交付时显示
- python - 如何向字典中添加新数据