Unhandled rejection CastError: Cast to ObjectId failed for value "catalog" at path "_id" for model "Blog"

  • 分类:【Nodejs
  • 浏览【874】
  • 评论【0】
  • 更新【2017-8-23 11:20:12】

Unhandled rejection CastError: Cast to ObjectId failed for value "catalog" at path "_id" for model "Blog"
    at MongooseError.CastError (e:\workspace\yuedun_ts\node_modules\mongoose\lib\error\cast.js:27:11)
    at ObjectId.cast (e:\workspace\yuedun_ts\node_modules\mongoose\lib\schema\objectid.js:149:13)
    at ObjectId.SchemaType._castForQuery (e:\workspace\yuedun_ts\node_modules\mongoose\lib\schematype.js:1064:15)
    at ObjectId.castForQuery (e:\workspace\yuedun_ts\node_modules\mongoose\lib\schema\objectid.js:189:15)
    at ObjectId.SchemaType.castForQueryWrapper (e:\workspace\yuedun_ts\node_modules\mongoose\lib\schematype.js:1021:15)
    at cast (e:\workspace\yuedun_ts\node_modules\mongoose\lib\cast.js:269:32)
    at Query.cast (e:\workspace\yuedun_ts\node_modules\mongoose\lib\query.js:3103:12)
    at Query._castConditions (e:\workspace\yuedun_ts\node_modules\mongoose\lib\query.js:1144:10)
    at Query._findOne (e:\workspace\yuedun_ts\node_modules\mongoose\lib\query.js:1346:8)
    at e:\workspace\yuedun_ts\node_modules\mongoose\node_modules\kareem\index.js:250:8
    at e:\workspace\yuedun_ts\node_modules\mongoose\node_modules\kareem\index.js:23:7
    at nextTickCallbackWith0Args (node.js:489:9)
    at process._tickCallback (node.js:418:13)
From previous event:
    at Query.exec (e:\workspace\yuedun_ts\node_modules\mongoose\lib\query.js:2897:17)
    at Query.then (e:\workspace\yuedun_ts\node_modules\mongoose\lib\query.js:2945:15)
    at Object.default_1 [as default] (e:\workspace\yuedun_ts\utils\viewer-log.ts:21:10)
    at e:\workspace\yuedun_ts\app.ts:53:14
    at Layer.handle [as handle_request] (e:\workspace\yuedun_ts\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:317:13)
    at e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:284:7
    at param (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:354:14)
    at param (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:410:3)
    at next (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:275:10)
    at e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:635:15
    at next (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:260:14)
    at Function.handle (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:174:3)
    at router (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (e:\workspace\yuedun_ts\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:317:13)
    at e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:335:12)
    at next (e:\workspace\yuedun_ts\node_modules\express\lib\router\index.js:275:10)
    at e:\workspace\yuedun_ts\node_modules\express-session\index.js:489:7
    at e:\workspace\yuedun_ts\node_modules\connect-mongo\lib\connect-mongo.js:305:11
    at handleCallback (e:\workspace\yuedun_ts\node_modules\mongoose\node_modules\mongodb\lib\utils.js:120:56)
    at e:\workspace\yuedun_ts\node_modules\mongoose\node_modules\mongodb\lib\collection.js:1417:5
    at handleCallback (e:\workspace\yuedun_ts\node_modules\mongoose\node_modules\mongodb\lib\utils.js:120:56)

乍看之下有用的只有第一行,意思是将catalog赋值给_id,但是这么一大片都是mongoose相关的错误,找不到在哪爆出来的错误,再仔细看一下,其实还是指出了具体错误代码:

    at Object.default_1 [as default] (e:\workspace\yuedun_ts\utils\viewer-log.ts:21:10)
    at e:\workspace\yuedun_ts\app.ts:53:14

最后找出了答案,model.findById(id),id的值为catalog导致的

关键字词:mongoose