-2

I am getting "Can't set header after they are sent " error on my nodejs page when I call $http.post from a page after loading, The issue is familiar but somebody could explain how to get rid of this without changing the current flow(coz it is necessary for my project)

Step 1:

app.get('/link/:params?', requireRole(["admin"]) , function (req, res){
//Render ejs template file here
var callback = function(meetingData) {      
res.render('chats/room', { 
                         title: 'Website Title Connect' , 
                         meetingData : meetingData, 
                         success:  req.flash('formSuccess'), 
                         session : req.session,
                         currentTime : new Date().getTime()

        });

}
.
.
.
callback(meetingData);


});

Step1 render the layout file (ejs)

  <% include ../layout/dashboard_header %>
  <div class="dashboard-container" ng-controller="ChatController">
  <div id="dynamicContent" ng-init="initChatRoom(<%= meetingData.current._id %>)">

  </div>
  </div>
  <% include ../layout/dashboard_footer %>

Step2: I have called ng-init from the loaded template using angular $http.post which results in Can't set header error . Code is given below

Angular code

$scope.initChatRoom = function(RoomId){
  $http.post('/api/meeting/roomdetails', {"RoomId":RoomId}).then(function (response) {

  console.log(response);

  });
}

Nodejs server code

app.post('/api/meeting/roomdetails', requireRole(["admin","facilitator"]) , function (req, res){


    var roomData        = req.body;
    var response        = {};
    response.success = 0;
    console.log(roomData);


    res.json(response);

});
Ajith
  • 2,476
  • 2
  • 17
  • 38
  • Possible duplicate of [Error: Can't set headers after they are sent to the client](http://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – Ben Fortune Mar 24 '17 at 12:11
  • Possible duplicate of [NodeJs Can't set headers after they are sent](http://stackoverflow.com/questions/32734671/nodejs-cant-set-headers-after-they-are-sent) – AJS Mar 24 '17 at 12:12
  • I have called angular http request from already rendered layout, which cause the issue. I couldnt find similar issues in the above post – Ajith Mar 24 '17 at 12:57

1 Answers1

0

The Can't set header after they are sent error happens when you try to return a response twice in a request handler in Express and other frameworks.

It is likely that the problem is in this part of your code:

.
.
.

that you didn't include.

It's usual that sometimes people forget to return from the handler after sending the error response and then the success response is sent again, triggering that error.

Look for your code - entire code, not only what you posted here - and see when something can be sent twice to the client and eliminate that.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • Sorry, I only mean for setting some data to meetingData , It can be ignored. – Ajith Mar 24 '17 at 12:59
  • Step 1 render a layout ( ejs file ) from where I calls an angular function using ng-init . From angular I have called $http.get('/api/meeting/roomdetails', {"RoomId":RoomId}).then(function (response) { console.log(response)}); Here the error happens – Ajith Mar 24 '17 at 13:01