res.send can only be called once. In the bcrypt.compare callback, res.send is called twice (for both statuses 201 and 200). Also, you must use a return statement after res.send or return res.send. This is because the code continues after executing the function.
You can also:
Change the status code for a false result to 400 – best practice
res.status().send() doesn’t exit the function like return, so you cannot use it like return. You are getting this error because you are sending a response more than once.
I would suggest structuring your logic to build a Response object, then send the response once it is built.
2
Answers
res.send
can only be called once. In thebcrypt.compare
callback,res.send
is called twice (for both statuses201
and200
). Also, you must use a return statement afterres.send
or returnres.send
. This is because the code continues after executing the function.You can also:
400
– best practice500
as it is never reached.I have applied these changes to my code below.
Fixed code:
res.status().send()
doesn’t exit the function likereturn
, so you cannot use it likereturn
. You are getting this error because you are sending a response more than once.I would suggest structuring your logic to build a
Response
object, then send the response once it is built.Example Response Object:
Example Usage: