I want to discuss the code I am currently working on:
const checkForRecord = async (id) => {
let model = mongoose.model('User');
let query = {}
query.dummy = false; <== This particular field is causing an error intentionally, as it does not exist in my User model.
let result = await model.findById(id, query);
console.log('This part of the code is being reached !!!');
console.log(result);
}
However, I encounter the following error:
(node:6680) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CastError: Cast to ObjectId failed for value ...
In addition, my console.log
statements do not seem to execute.
Why is this error not being caught in the result variable, considering that my function is asynchronous?
I have tried both approaches:
let result = await model.findById(id, query);
and
let result = await model.findById(id, query).exec();
Yet, the outcome remains the same.