0

I have created a simple login api, when I try texting the api in postman am getting the following error: Error: Can't set headers after they are sent to the client using node js

code that I have written is as follows:

//verify user
exports.verifyuser = (req, res) => {
    User.findOne(req.body.email)
    .then(user => {
        if(!user) {
            return res.status(404).send({
                message:"no user with email id found"
            });            
        }
        res.send(user).then(function(res){
            console.log('verify db res',res)
        });
    }).catch(err => {
        if(err.kind === 'ObjectId') {
            return res.status(404).send({
                message: "Note not found with id "
            });                
        }
        return res.status(500).send({
            message: "Error retrieving note with id "
        });
    });
}; 

Being new to express am unable to recognize where have I gone wrong, also am not getting the correct response like below : enter image description here

here I have added abc3@gmail.com to the url but am getting response for aaaa@gmail.com

Kindly help me out here, any suggestions would be very helpful. Thank you in advance.

TRINA CHAUDHURI
  • 627
  • 1
  • 11
  • 42
  • Please take a look at this link, which is probably the best answer to your problem. https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client – ChillAndCode Mar 12 '21 at 12:38
  • this did not help,as you can see I have nowhere used res.setheader() – TRINA CHAUDHURI Mar 12 '21 at 12:52
  • This happens when you close the connection and then you try to send more content. If `res.send()` closes the connection you cannot call it again in the `.catch{}` – Ulises Mar 13 '21 at 07:37

0 Answers0