To learn Node.js I'm using VS Code Win10 to create an express Simple Client Request. The problem I'm running into is when I try to send an error 404 the Simple client request returns the text 'object object'. Non-of the attempts to return a not found message works. Can you let me know what is wrong with the below get function
Thanks
router.get('/accounts/:user', (req, res) => {
const user = req.params.user;
console.log('at 2 ' + user); **<<-- works**
const account = db[user];
if (!account) {
console.log(user + ' does not exist'); **<<-- works**
return res.json('user does not exist'); **<<-- works**
// return res.status(404).send({ message: 'Route'+req.url+' Not found.' }); **<<-- does not works**
return res.status(404).json( { error: 'user does not exist'} ); **<<-- does not works**
}
return res.json(account); **<<-- works**
});
you are attempting to set the status header after response is sent to clientreturn res.json('user does not exist');so linereturn res.status(404).json( { error: 'user does not exist'} );is not being called. And if you remove thereturnfrom previous line you will get theattempting to set status after response is senterrorjson({ error: '....'})the client reads an object (thejson) and in that another object (error), so outputsobject object. Use.send('message')like in the answer below