'Unable to handle the errors in node.js
I am new to node.js. I am trying incorporate error handling in the below code. I need to throw the error if I am not passing the parameter. One is if the tag parameter is empty another one is if I am not finding the tag field in the collection while passing.
case 1:
{ "likes" : 970
"share" : 345
"tag" : ""
} // throw error
case 2:
{
"likes" : 970
"share" : 345
} // throw error
app.get('/api/ping/:tag', (req,res) => {
const dept = req.params.tag;
console.log(dept)
if(!dept) {
res.json({error: "Provide Tag parameter to search"})
} else {
const blogData = blogPosts.filter((d) => d.tag == dept)[0];
res.status(200).send({ok:true, blogData})
}
})
Can anyone help me on this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|