简体   繁体   English

在检查 mongodb 中的错误类型时我得到了未定义

[英]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类型的错误,它的属性中没有列出kindhttps://mongoosejs.com/docs/api.html#mongoose_Mongoose-CastError

So this seems like working as intended.所以这似乎按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能在 Javascript 中操作这个数组来进行这种错误检查? - How could I manipulate this array in Javascript for this kind of error checking? JavaScript运行时错误:检查是否未定义时,'variable'未定义 - JavaScript runtime error: 'variable' is undefined while checking to see if undefined 我收到错误“无法读取未定义的属性‘then’” - i got error " Cannot read property 'then' of undefined" TypeError: Cannot read property 'then' of undefined -- 在使用 then 时出现此错误 - TypeError: Cannot read property 'then' of undefined --got this error while using then 遇到 API 时出现验证错误 - I got a validation error while hitting an API 从 mongodb 检索数据时出现错误 - I got an error when i retrieve data from mongodb Nestjs-在从 mongodb 以 json 格式获取数据时得到“未定义”为 output - Nestjs- Got 'undefined' as output while fetching data from mongodb in json format 检查未定义导致未定义错误 - Checking for undefined causes undefined error 错误连接 MongoDB:错误:Route.get() 需要一个回调函数,但得到一个 [object Undefined] - Error connection MongoDB: Error: Route.get() requires a callback function but got a [object Undefined] 我收到此错误 => TypeError: Cannot read property 'image' of undefined - I got this error => TypeError: Cannot read property 'image' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM