angular - Retrieve data from an asynchronous service in a component -


i retrieve data firebase in function of service asynchronously , retrieve them in component.

i have data in database, function returns data can not retrieve them in home.ts (array empty).

screen of console.log

todolistservice.ts :

    import {injectable} '@angular/core'; import {angularfiredatabase, firebaselistobservable} 'angularfire2/database'; import * moment 'moment'; import 'rxjs/add/operator/map'; import {observable} "rxjs/observable";  @injectable() export class todolistservice {    statuts: array<any> = [];   dateoftheday: string;    constructor(public afdb: angularfiredatabase) {     moment.locale('fr');     this.dateoftheday = moment().format('l'); // date au format : 04/07/2017   }    /**    *    * @returns {observable<array<any>>}    */   statustoshow():observable<array<any>> {     let localstatuts: array<any> = [];     return this.afdb.list('/statut').map(status => {       (let s of status) {         if (this.dateoftheday === s.statut_date_tache) {           if (s.statut_id_tache in this.statuts === false) {             localstatuts[s.statut_id_tache] = s;             console.log('=== statustoshow ===');             console.log(localstatuts);             return localstatuts;           }         }       }     });   } } 

home.ts :

    import {component} '@angular/core'; import {modalcontroller, navcontroller} 'ionic-angular'; import {todolistservice} "../../providers/todolistservice";  @component({   selector: 'page-home',   templateurl: 'home.html' }) export class homepage {    public statusoftheday: array<any> = [];    constructor(public navctrl: navcontroller, public modalctrl: modalcontroller, public todolistservice: todolistservice) {    }    ionviewdidload() {     this.todolistservice.statustoshow().subscribe(status => this.statusoftheday = status);     console.log('=== home ===');     console.log(this.statusoftheday);   } } 

i not know problem comes .. normal "=== home ===" first appears in console ?

thank in advance , thank @ajt_82.

i think because not returning .map. make simpler filter statement.

return this.afdb.list('/statut').map(status => {   return status.filter(s => this.dateoftheday === s.statut_date_tache && (s.statut_id_tache in this.statuts === false)); }); 

if still want log out status selected use

return this.afdb.list('/statut').map(status => {   return status.filter(s => {     if (this.dateoftheday === s.statut_date_tache && (s.statut_id_tache in this.statuts === false))) {       console.log (s);       return true;     }     return false;   } }); 

you need wait subscribe finish before logging

ionviewdidload() {   this.todolistservice.statustoshow().subscribe(status => {     this.statusoftheday = status;     console.log('=== home ===');     console.log(this.statusoftheday);   }); } 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -