首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我得到了am CastError: ObjectId的角色转换失败了?

为什么我得到了am CastError: ObjectId的角色转换失败了?
EN

Stack Overflow用户
提问于 2022-03-12 15:09:11
回答 1查看 222关注 0票数 0

当我在我的项目中调用一条路线时,我会得到以下信息:

代码语言:javascript
复制
serveur running PORT: 7000

/user/new
CastError: Cast to ObjectId failed for value "favicon.ico" (type string) at path "_id" for model "User"
    at model.Query.exec (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\query.js:4650:21)
    at model.Query.Query.then (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\query.js:4749:15)
    at exports.findOneUser (C:\Users\Admin\Downloads\books\routes\homeController.js:38:6)    
    at Layer.handle [as handle_request] (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\route.js:137:13) 
    at Route.dispatch (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Admin\Downloads\books\node_modules\express\lib\router\index.js:281:22        
    at param (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\index.js:360:14)    at param (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\index.js:371:14) {
  messageFormat: undefined,
  stringValue: '"favicon.ico"',
  kind: 'ObjectId',
  value: 'favicon.ico',
  path: '_id',
  reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
      at new BSONTypeError (C:\Users\Admin\Downloads\books\node_modules\bson\lib\error.js:41:28)
      at new ObjectId (C:\Users\Admin\Downloads\books\node_modules\bson\lib\objectid.js:65:23)
      at castObjectId (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\cast\objectid.js:24:12)
      at ObjectId.cast (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\schema\objectid.js:247:12)
      at ObjectId.SchemaType.applySetters (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\schematype.js:1180:12)
      at ObjectId.SchemaType._castForQuery (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\schematype.js:1614:15)
      at ObjectId.SchemaType.castForQuery (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\schematype.js:1604:15)

我有一个路由(如route.get("/", myCallBack)),它返回我的mongodb数据库中的所有对象。我找不到我的错误在哪里。

EN

回答 1

Stack Overflow用户

发布于 2022-03-13 06:46:30

问题是它不是有效的_id字符串。它必须是12字节二进制字符串或24十六进制字节字符串."favicon.ico“两者都不是。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71450724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档