M
M
mDrama2020-06-28 18:27:15
MongoDB
mDrama, 2020-06-28 18:27:15

Why is the "name" property missing in the error from Mongoose?

Good afternoon. Trying to do normal error handling on a server connected to Mongo. To do this, I need to recognize errors related to the DB itself. For example, when I try to get an item by id, but the id is invalid.

Like this: 127.0.0.1:8000/api/v1/tours/ssssss - in this case, ssssss is the ID.

I'm supposed to do this by checking the name property on the object with the error: if (error.name === 'CastError'). But I don't have the name property in the error.

{
    "status": "error",
    "error": {
        "stringValue": "\"ssssss\"",
        "kind": "ObjectId",
        "value": "ssssss",
        "path": "_id",
        "reason": {},
        "statusCode": 500,
        "status": "error"
    },
    "message": "Cast to ObjectId failed for value \"ssssss\" at path \"_id\" for model \"Tour\"",
    "stack": "CastError: Cast to ObjectId failed for value \"ssssss\" at path \"_id\" for model \"Tour\"\n    at model.Query.exec (D:\\IT\\Node-Natours\\node_modules\\mongoose\\lib\\query.js:4351:21)\n    at model.Query.Query.then (D:\\IT\\Node-Natours\\node_modules\\mongoose\\lib\\query.js:4443:15)\n    at process._tickCallback (internal/process/next_tick.js:68:7)"
}


At the same time, in another case, when I create a document with an already existing name, this field appears there.

At the moment I have done a check on this field: "kind": "ObjectId". But I don't know if there will be gaps in the future because of this.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question