I have a route that is /mysafe/idofthemodel. When the idofthemodel isn't found it throws a cast error Cast to ObjectId failed for value "something" (type string) at path "_id" for model "modelname". Instead of the error I would like to have a 404 error.
Here is my route
app.get('/mysafe/:safeid',isLoggedIn,isAuthor, async(req,res)=> {
const { safeid } = req.params;
const safe=await Safe.findById(safeid).populate('author')
const password = await Password.find({safe:safeid}).populate('safe')
res.render('passwords/index', { password,safe })
})
Schemas:
const PasswordsSchema = new Schema({
title: String,
url: String,
password: String,
safe: {
type: Schema.Types.ObjectId,
ref: "Safe"
},
})
const SafeSchema = new Schema({
author: {
type: Schema.Types.ObjectId,
ref: "User"
},
})