这里需要一些帮助。 几乎尝试了我在互联网上找到的所有方法,但没有成功。
所以,这是我的代码:
const id = req.params.id.trim();
Blog.findById(id)
.then((result) => {
res.render("details", { blog: result, title: "Blog Details" });
})
.catch((err) => {
console.log(err);
});
};
当我尝试创建新博客时会发生这种情况。
这是我的错误:
CastError: Cast to ObjectId failed for value "blogs" (type string) at path "_id" for model "Blog"
at model.Query.exec (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/query.js:4546:21)
at model.Query.Query.then (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/query.js:4645:15)
at blog_details (/Users/xxx/blogMongoDB/controllers/blogController.js:19:10)
at Layer.handle [as handle_request] (/Users/xxx/blogMongoDB/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/xxx/blogMongoDB/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/xxx/blogMongoDB/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/xxx/blogMongoDB/node_modules/express/lib/router/layer.js:95:5)
at /Users/xxx/blogMongoDB/node_modules/express/lib/router/index.js:281:22
at param (/Users/xxx/blogMongoDB/node_modules/express/lib/router/index.js:354:14)
at param (/Users/xxx/blogMongoDB/node_modules/express/lib/router/index.js:365:14) {
messageFormat: undefined,
stringValue: '"blogs"',
kind: 'ObjectId',
value: 'blogs',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
at new BSONTypeError (/Users/xxx/blogMongoDB/node_modules/bson/lib/error.js:41:28)
at new ObjectId (/Users/xxx/blogMongoDB/node_modules/bson/lib/objectid.js:65:23)
at castObjectId (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/cast/objectid.js:25:12)
at ObjectId.cast (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/schema/objectid.js:245:12)
at ObjectId.SchemaType.applySetters (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/schematype.js:1135:12)
at ObjectId.SchemaType._castForQuery (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/schematype.js:1567:15)
at ObjectId.SchemaType.castForQuery (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/schematype.js:1557:15)
at ObjectId.SchemaType.castForQueryWrapper (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/schematype.js:1534:20)
at cast (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/cast.js:336:32)
at model.Query.Query.cast (/Users/xxx/blogMongoDB/node_modules/mongoose/lib/query.js:4969:12),
valueType: 'string'
}
您的请求参数
must be a string of 12 bytes or a string of 24 hex characters
检查请求参数中的 _id
。它必须是 24 十六进制数字,如下所示:61d634706a98a61edd42bf45
这可能会解决您的问题。如果有人可以提供有关此主题的更多信息,那就太好了。
问题是:如果客户端搜索长度不是 24 的
_id
怎么办?为什么 Mongoose 会抛出错误而不是只返回“id not found”之类的内容?
将 id 类型从 string 转换为 mongoose objectID(默认为 _id)。
const id = mongoose.Types.ObjectId(req.params.id.trim());
mongodb _id
是一个 ObjectID
,因此猫鼬需要查找对象而不是字符串。
很好的解决方案:
const id = mongoose.Types.ObjectId(req.params.id.trim());
您的处理程序
app.get('/blogs/create')
需要走到您的处理程序app.post('/blogs/:id')
上方。
CastError: Cast to ObjectId failed for value "blogs" ==> 这意味着“const id = req.params.id.trim()”的实际值是字符串“blogs”,而这个“blogs”不是Blog.findById(id) 所期望的 12 个字节的字符串或 24 个十六进制字符的字符串。
https://mongoosejs.com/docs/schematypes.html#objectids
尝试检查 id 的值,然后确保从前端发送正确的值
将
id
转换为 ObjectId
:
import { ObjectId } from "mongodb";
const convertedId = ObjectId(id)
如果由于无效
id
而转换不成功,那么你可以捕获错误:
try{
//your code
}catch(e){
if(e instance of BSONTypeError){
//do whatever you need if this occurs
}
}
别忘了
import { BSONTypeError } from "bson";
就我而言..
当我应该添加一个字符串时,我却添加了一个对象。
const id = req.params
这段代码给了我一个对象,所以我必须选择该对象内的 id。
const id = req.params.streamId;
如果还是不行,就在最后加上
.toString()
将 id 类型从 string 转换为 mongoose objectID(默认为 _id)。
const id = mongoose.Types.ObjectId(req.params.id.trim());
您无法将普通字符串转换为 ObjectId,请在使用前检查
param.id
。
您可以使用
ObjectId.isValid("YOUR ID STRING")
来测试它。
我曾经遇到过类似的问题 如果您有处理程序
app.get('/blogs/create')
将其放在处理程序上方 app.get('/blogs/:id')
。
此错误是由于 Express 试图将 url
create
中的字符串类型解释为 id。