Saturday 22 February 2020

Can I get someone to give me their opinion about this code and it's error handling?

Im using my weekends to get better with NodeJs/Express. I don't work with anyone who knows NodeJS at a professional level so I am looking to the fine people of r/node for criticism!Outline:I have created a user in my Mongo database. In this example I am signing that user in, and responding with a valid JWT token upon a successful password match.It requires a few async functions: mongoose.findOne(), bCrypt.compare(), and jwt.sign() and each one of these functions can return an error as most know. In the code block below, I am nesting bCrypt.compare() inside of findOne(), and then jwt.sign() inside of bCrypt.compare().My main questions here are with the error handling. I don't feel like this is right, especially with all of return next() 's being used in the catch blocks. I suppose their might be a better way, but at my current abilities, it seems like the best way to handle each different error condition. For all I know, there is nothing wrong with what I am doing here.Here is the sign in controller: https://pastebin.com/DhU760mcHere is my generic error handler called by next(): https://pastebin.com/YxhA6cTNHow would a more professional level of understanding handle this?

Submitted February 22, 2020 at 06:53PM by skidmark_zuckerberg

No comments:

Post a Comment