angular - Convert observable to promise and manipulate result -


i want calculate total price set of items returned firebase database observable. however, service returns nothing.

here's tried:

service:

  getrooms(): {     let result = {};     this.items       .topromise()       .then(items => {         (let item of items) {           let name = item.name;           let price = item.price;           if (name in result) {             result[name] += price;           } else {             result[name] = price;           }         }       });     return result;   } 

component

export class inspirationcomponent implements oninit {    rooms: any;    constructor(private inspirationservice: inspirationservice) {     this.items = this.inspirationservice.getrooms();   }    ngoninit() {   }  } 

well starters shouldn't populate variable this.items that. should this:

component

export class inspirationcomponent implements oninit {    rooms: any;    constructor(private inspirationservice: inspirationservice) {     this.inspirationservice.getrooms().then(result => {          this.items = result ;     });   } 

service

 getrooms(): {     return this.http.get('url')       .map(items => {          let result = {}          (let item of items) {           let name = item.name;           let price = item.price;           if (name in result) {             result[name] += price;           } else {             result[name] = price;           }         }          return = result ;     }).topromise();   } 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -