Firebase : issue when retrieving data


#1

Hi,

I’m facing a problem with retrieving data from Firebase in my app.

First, i’ve created a provider like below

    import {Injectable} from '@angular/core';
    import {Observable} from 'rxjs/Observable';
    import {Http} from '@angular/http';
    import 'rxjs/add/operator/map';

    @Injectable()
    export class FirebaseService {
        baseRef = new Firebase('https://project-<my_id>.firebaseio.com/');
        _data;
        constructor(public http: Http) {
            this._data = {};
        }

        // Retrieving user list
        getData(){
            
            this.baseRef.on("value", 
            (snapshot) => {
                let dataSet = snapshot.val();
                this._data = dataSet.users;
                console.log("my user list : " + JSON.stringify(this._data));
                return this._data;
            },
            (error) => {
            console.log("Error : " + JSON.stringify(error));
            });
            
        }
    }

Then i create a form page to retrieve my firebase data

    import {Component} from '@angular/core';
    import {NavController, NavParams, Platform} from 'ionic-angular';
    import {FirebaseService} from '../../providers/firebase-service/firebase-service';

    @Component({
    templateUrl: 'build/pages/fiche/fiche.html',
    providers: [FirebaseService]
    })

    export class FormPage {
        public baseRef;
        public users: any[];
        
        constructor(public nav: NavController,platform: Platform, public fbS: FirebaseService) {
            this.baseRef = fbS;
            this.users = this.baseRef.getData()
        }
    }

My issue is that in my Form page, i can’t retrieve my user list from my Firebase.
But when i log retrieved data in my getData function, i’ve all my users. But when i call getData function from my Form page, the result (this.users) is empty…

But, if i return directly an array like [{id:1, user:“mike”},{id:2, user:“tom”}], it works !
I think i’ve a problem with the .on() function of firebase object or simply a code missing…

Anybody got an idea ?


#2

Hi, after long try, i think that there is a coding error on using provider from a controller…I tried a lot of sample code found on the Web but my issue is still there :confused: