Answer the question
In order to leave comments, you need to log in
How to return a message if nothing is found?
I'm running an ExpressJS server. There is a handler:
router.get('/articles/:section', Article.getArticlesBySection)
exports.getArticlesBySection = (req, res, next) => {
Article.find({section: req.params.section})
.then((articles) => {
if (!articles) {
// FIX THIS
return res.status(404).json({
message: 'Articles doesn\'t exist'
})
}
return res.status(200).json(articles)
})
.catch((err) => {
return next(err)
})
}
section
. I want to return a message to the user if there is a call to a route that doesn't match my section
. The problem is that if .find
it doesn't find anything, then it doesn't work.if (!articles) {
// FIX THIS
return res.status(404).json({
message: 'Articles doesn\'t exist'
})
}
[]
.findOne
works as intended with a similar code (I use it if the address of a specific note is wrong.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question