Nodejs not always showing errors

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Nodejs not always showing errors



I'm starting to learn js and nodejs. I had an issue which took me a long time to resolve.



I was using bcrypt library and i made a typo on require statemant:



const bcrpyt = require('bcryptjs');


const bcrpyt = require('bcryptjs');



then on my pre save schema i used:


bcrypt.hash(user.password, 10, (err, hash) =>
user.password = hash;
next();
);



so bcrypt was undefined


bcrypt


undefined



on my route I had:


user.save()
.then(/*some action*/)
.catch(e => res.status(400).send(e));



so after making a request, I got status 400 but e was empty object


e




Any idea why there was no error such as "calling hash on undefined" or something like that?





have you tried console.log(e) ? Do you get the same thing?
– boyd
Aug 6 at 9:58


console.log(e)





Did you try throwing the err of bycrypt.hash if there is any?
– imans77
Aug 6 at 9:58


throw


err


bycrypt.hash





@boyd omg console.log(e) shows exactly the error. I thought send(e) would show the same which was clearly an mistake... Thanks. Is there any configuration on node to set to show such errors on response? @imans77 bcrypt.hash didn't give any error because it wasn't even executed due to a typo
– user3923317
Aug 6 at 10:24



console.log(e)


send(e)


bcrypt.hash




1 Answer
1



You can send only the message to the client:



res.status(400).send(e.message)


res.status(400).send(e.message)



I don't know if you are using express.js or plain node.js but if you want to see these errors on the client side you can wrap the entire server request code in a try catch block like so(using vanilla nodejs):


http.createServer(function(request, response)
try
response.writeContinue(undefinedVariable);
catch (error)
response.status(400).end(error.message);

).listen(port);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS