1
I’m doing an error treatment that if a wrong id is passed by url
he returns a error 400, but when I take the test it returns a error 200 one more message
"error": "Error", "message": "Cast to ObjectId failed for value \"5d69f4aa827b491c678c50a\" at path \"_id\" for model \"Product\""
Follow the controller:
router.get('/product-view/:id', async (req, res) => {
const id = req.params.id
try{
if(!id) return res.status(404).send({ error: 'Produto não encontrado'})
console.log(id);
const product = await Product.findById(id);
res.send(product);
}catch(error){
res.send({
error: 'Error',
message: error.message
})
}
});
Product.findById
apparently the error is here ... you use which database?– novic
I use Mongo DB
– Edgar Silva
Status
200
is not error, but success, you need to call the methodstatus
with the return code. Ex.res.status(400).send({error: 'Error', message: error.message})
– Denis Rudnei de Souza