When trying to set up my workoutId param, I encounter the following error:
UnhandledPromiseRejectionWarning: CastError: Casting to ObjectId failed for value "5fb02bd8b61abc02" at path "_id" for model "Workout"
If the workoutId exists, I can successfully make a GET request. However, when attempting with an id that doesn't exist, it should result in a 404 response but it does not. Am I overlooking something?
workoutsRouter.param('workoutId', async (req, res, next, workoutId) => {
try {
await mongoose.connect('mongodb+srv://nalanart:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="640c0d000d0a0314051717130b16002407081117100116544a560d14080c4a090b0a030b00064a0a0110">[email protected]</a>/workout-app-db?retryWrites=true&w=majority',
{ useNewUrlParser: true, useUnifiedTopology: true })
const workout = await Workout.findById(workoutId).exec()
if(Object.keys(workout).length === 0) {
res.sendStatus(404)
} else {
req.workout = workout
next()
}
} catch(error) {
throw error
}
})