The problem is more related to the typescript. I need these methods to check the returned result after querying the db.
const user = await User.findOne({idUser: 1}).isAbsent(400)// this status code http
I would like to ask if it is a good practice that has created such methods. The point of this method is to reduce the amount of code written, since there is no need to check the existence of the returned document and return an exception. If the document is not found, an exception is raised. I also need access to the context from the isAbsent() method to the request in order to get the name of the model through which the request was made
I tried to do this through plugins that are in mongoose, but alas I did not find a way to extend the Query class type.
isAbsent(schema: Schema) {
schema.query.isAbsent = function(500) {
if (!document) {
throw ApiError(500)
}
return document
}
}
The error occurred: Property 'isAbsent' does not exist on type '{}'
. This question is similar to @markitusss question, but I use typescript
Jaspper15325 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.