I have the following code:
const userSchema = new mongoose.Schema({
name: String,
loginAttempts: [ { IP: String } ]
})
const User = mongoose.model('User', userSchema, 'users')
app.get('/users/:userId/login-attempts', async(req,res,next)=>{
const loginAttempt = await User.aggregate([
{ $match: { _id: mongoose.Types.ObjectId(req.params.userId) } },
{ $unwind: '$loginAttempts' },
{ $replaceRoot: { newRoot: '$loginAttempts' } },
])
})
I want when the operator $match does not find a matching document to throw an error to report something at least, to show the client a real message that the req.params.id he's looking for does not exist in the first stage.
But how?