2

I am new to node and using stormpath for custom data, my server code retrieves the accounts in a group with the custom data for each. It should send data to client.. However I am getting error.. Any kind of help will be helpful, thanks.

Error : "Can't set headers after they are sent."

app.post('/profile/cards', bodyParser.json(),ExpressStormpath.loginRequired,function(req, res) {
  var href="https://api.stormpath.com/v1/groups/2D48mI3C9uBFJAFW3pp7Kv";
   client.getGroup(href,function(err,group){
      group.getAccounts(function(err,accounts){
        accounts.each(function(account,cb){
          account.getCustomData(function(err,CustomData){
           res.send(CustomData);
        });
          cb();
        },function(err){
          console.log('Finished iterating over accounts');
        });
      });
    });
  });

1 Answers1

1

Your code isn't working because you're iterating over Groups in Stormpath and attempting to return a web page to the user (res.send) on each call which is NOT allowed.

You can only call res.send() a single time per route.

rdegges
  • 32,786
  • 20
  • 85
  • 109
  • I want to retrieve custom data for all users in a group... once button is clicked... So should i store custom data with each iteration in a json and then send it ? – Kshitiz Sharma Jan 11 '16 at 15:30
  • That's correct -- store your data in one large JSON array, then send it down at the very END. – rdegges Jan 11 '16 at 18:45