mongoose 二级 type 错误报错 500 而不是 422

来源:12-6 用户属性中的话题引用

木恩丶

2019-08-09

比如说 educations 中的 diploma 字段 如果我输入的是 “str” 返回的是 500 错误 而不是 "message": "Validation Failed" , 没有查到对应解决方法

 "stack": "CastError: Cast to embedded failed for value \"{ school: \\'5d4d0fd085e62da0ea6c5bf8\\',\\n  major: \\'5d4d123a7b8ad9a18d3e01e8\\',\\n  diploma: \\'e\\' }\" at path \"educations\"\n    at new CastError (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/error/cast.js:29:11)\n    at DocumentArray.cast (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/schema/documentarray.js:408:19)\n    at DocumentArray.SchemaType.applySetters (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/schematype.js:948:12)\n    at DocumentArray.SchemaType._castForQuery (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/schematype.js:1362:15)\n    at DocumentArray.SchemaType.castForQueryWrapper (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/schematype.js:1329:17)\n    at castUpdateVal (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/helpers/query/castUpdate.js:451:19)\n    at walkUpdatePath (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/helpers/query/castUpdate.js:278:22)\n    at castUpdate (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/helpers/query/castUpdate.js:80:18)\n    at model.Query._castUpdate (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/query.js:4405:10)\n    at castDoc (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/query.js:4433:18)\n    at model.Query.Query._findAndModify (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/query.js:3401:19)\n    at model.Query.<anonymous> (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/query.js:2949:8)\n    at model.Query._wrappedThunk [as _findOneAndUpdate] (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)\n    at process.nextTick (/Users/mac/Desktop/jevonsOfCode/toLearnKoa/node_modules/kareem/index.js:369:33)\n    at process._tickCallback (internal/process/next_tick.js:61:11)",
 "message": "Cast to embedded failed for value \"{ school: \\'5d4d0fd085e62da0ea6c5bf8\\',\\n  major: \\'5d4d123a7b8ad9a18d3e01e8\\',\\n  diploma: \\'e\\' }\" at path \"educations\"",

错误信息如上,课程还没看完 不知道后面是否有解决方法 QAQ

写回答

1回答

lewis

2019-08-09

搜一下message里的错误信息,你就知道了,其实就是mongodbID错了。

0
4
木恩丶
回复
lewis
我知道这个是什么问题了, 写 ctx.verifyParams 的时候,对与 array 类型,就写到最外层,没有校验内容是什么 我暂时也没有找到这个内部的写法 B: [{ C: {type: 'number', required: true } }] 这样也不可以 所以怎么校验内部呢
2019-08-13
共4条回复

Node.js仿知乎服务端-深入理解RESTful API

Koa2+MongoDB+Postman+JWT+阿里云

949 学习 · 331 问题

查看课程