node.js - Angular2 - Multiple service calls in OnInit method of component -


how can make 2 service calls in oninit() method of component ?

export class apartmentcomponent implements oninit {     public apartments: object[];     public temp: object[];      constructor(private apartmentservice: apartmentservice) {     this.apartmentservice = apartmentservice;     }      ngoninit() {     this.apartmentservice.getapartments().subscribe(res => this.apartments = res);      this.apartmentservice.getstats().subscribe(res => this.temp = res);      console.log(json.stringify(this.temp));     } } 

in service.ts

getapartments() {     return this.http.get('./api/businessunits/butype').map((res: response) => res.json()); }  getstats(){      console.log('request reached');     return this.http.get('./api/apartments/getstats').map((res: response) => res.json()); }  

in server.ts (expressjs)

router.route('/api/businessunits/butype')              .get(function(req, res) {     businessunit.find({unitid: {$exists: true}, unittype: {$exists:  true}},'unitid unittype',{sort:{unitid: 1}},function(err, businessunits) {         if (err)             res.send(err);          res.json(businessunits);      }); });  router.route('/api/apartments/getstats')              .get(function(req, res) {     //apartment.aggregate([{$match:{_id: "apttype"}},{$group:{_id:{apttype:"$apttype"},count:{$sum:1}}}],function(err, apartments) {       apartment.find('apttype',function(err, apartments) {         if (err)             res.send(err);         res.json(apartments);      }); });  

the getapartments() works fine individually when comment out getstats() method call.

i getting following error

error: can't set headers after sent. @ serverresponse.outgoingmessage.setheader (_http_outgoing.js:335:11) @ serverresponse.header (m:\workspace\angular2startkit\node_modules\express 

subscribing observables async operation, means schedules tasks done later.

when console.log(json.stringify(this.temp) executed, call server in getstats() (if makes 1 - assume does) wasn't sent, , therefor no response received yet.

it not clear code in question whether request getapartments() or getstats() sent first.

to preserve specific order in async operations, need chain them next executed when former completed.

if want print result of getstats() can done like

ngoninit() {   this.apartmentservice.getapartments().subscribe(res => this.apartments = res);    this.apartmentservice.getstats().subscribe(res => {     this.temp = res;     json.stringify(this.temp)   }); } 

alternatives

ngoninit() {   this.apartmentservice.getapartments().subscribe(res => this.apartments = res);    this.apartmentservice.getstats()   .map(res => this.temp = res);   .subscribe(temp => console.log(json.stringify(this.temp));   }); } 

or

ngoninit() {   this.apartmentservice.getapartments().subscribe(res => this.apartments = res);    this.apartmentservice.getstats()   .map(res => this.temp = res);   .topromise().then(temp => console.log(json.stringify(this.temp));   }); } 

if want chain 2 subscribes

this.apartmentservice.getapartments().subscribe(res => this.apartments = res); this.apartmentservice.getstats().subscribe(res => this.temp = res); 

there lots of possiblilities flatmap() depending on requirements. might want sent 1 after other completed, or send both possible wait both complete. there different ways handle errors, ...

for more details see http://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html