Skip to content
Advertisement

Error: Cannot remove headers after they are sent to the client

I was working on admin registration and admin data retrieving react app. The registration works fine but retrieving admin data is crushing my backend. I have encountered this error when call the given endpoint from my react app. But when I call it from Postman it works very fine. And when I see the console on my browser my react app sends two calls simultaneously instead of one. On these calls my app crushes. If any one can show me how to solve this problem? For backend = Node.js with express.js framework For frontend = React

This is the error I am getting

JavaScript

This is how I setup my endpoint and changed the data to a string in order to get simple response but it crushes

JavaScript

And this is the call from my react app

JavaScript

Advertisement

Answer

This particular error happens when you try to send more than one response for the same incoming request (something you are not allowed to do).

You are calling res.send() more than one for the same request on your server.

The first happens in the makeRequest() function.

The second time happens in this line of code:

JavaScript

You can’t do that. You get ONE response per incoming request. So, either send the response in makeRquest() and don’t send it in the caller. Or, don’t send the response in makeRequest() and just return what the response should be and let the caller send it. Pick one model or the other.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement