I am getting an error:
Error: Can't set headers after they are sent at
ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
My Api
exports.signUp = function(req,res,next) {
var userCredentials = new userModel.User({
user_name: req.body.userName,
email: req.body.email,
password: req.body.password
})
userLib.getUserByEmail(req.body.email, function (err,result) {
console.log(err,result)
if(result && err==null) {
res.json({status:"emailAlreadyRegistered"});
}
else {
userCredentials.save(function(error,result) {
if(error == "error"){
return res.status(500).json({status:"error"});
}
return res.status(200).json({status:"success"});
})
}
});
}
I find that the issue is in .save(()=>{})
. While checking with postman It works fine for first input.sent {status:"success"}
response.
Issue
If I send the request once again it will give an error Error: Can't set headers after they are sent
How do I fix it?
Thanks in advance