[英]I got undefined while checking error kind in mongodb
When I enter valid id after /user/ as 'http://localhost:5000/api/profile/user/5ee9fbe0e82023146c8a5230 ', it returns result as expected.当我在 /user/ 之后输入有效 ID 为“http://localhost:5000/api/profile/user/5ee9fbe0e82023146c8a5230 ”时,它会按预期返回结果。 But I need to check error kind if error occurs when user give invalid id.但是如果在用户提供无效 id 时发生错误,我需要检查错误类型。 So I intentionally entered invalid id as ' http://localhost:5000/api/profile/user/5ee9fbe0e82023146c8a52304 '.所以我故意输入了无效的 id 作为' http://localhost:5000/api/profile/user/5ee9fbe0e82023146c8a52304 '。 Now It throws the error as现在它抛出错误为
MongooseError [CastError]: Cast to ObjectId failed for value "5ee9fbe0e82023146c8a52304" at path "user" for model "profile"
at new CastError (D:\Programming\MERN\devConnector\node_modules\mongoose\lib\error\cast.js:29:11)
at model.Query.exec (D:\Programming\MERN\devConnector\node_modules\mongoose\lib\query.js:4341:21)
at model.Query.Query.then (D:\Programming\MERN\devConnector\node_modules\mongoose\lib\query.js:4433:15)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
messageFormat: undefined,
stringValue: '"5ee9fbe0e82023146c8a52304"',
kind: undefined,
value: '5ee9fbe0e82023146c8a52304',
path: 'user',
reason: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (D:\Programming\MERN\devConnector\node_modules\bson\lib\bson\objectid.js:59:11)
at castObjectId (D:\Programming\MERN\devConnector\node_modules\mongoose\lib\cast\objectid.js:25:12)
at ObjectId.cast
}
Now I want to check error.kind but there is undefined.现在我想检查 error.kind 但没有定义。 What's wrong there.那里有什么问题。
According to thedocumentation the kind
-property is only populated for ValidatorError
s.根据文档, kind
-property 仅为ValidatorError
填充。
In your case you get an error of type CastError
which does not have kind
listed in its properties: https://mongoosejs.com/docs/api.html#mongoose_Mongoose-CastError在您的情况下,您会收到CastError
类型的错误,它的属性中没有列出kind
: https://mongoosejs.com/docs/api.html#mongoose_Mongoose-CastError
So this seems like working as intended.所以这似乎按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.