lliance
lliance

Reputation: 47

Retrieving firebase real time data with Angular

I'm storing data in a Firebase realtime database and I'm trying to get the stored data and it seems I'm not getting it correctly - I need any assistance.

auth.service.ts:

 get_Data() {
  return firebase.database().ref('transfer/').on('value', (snapshot) => {
    this.transfer = snapshot.val();
     console.log('snapshot here',  this.transfer);
  })
 }

returns

snapshot here

app.component.ts:

 dataList(){
       this.list = this.authService.get_Data();
       console.log('got you', this.list)
    }

returning undefined ...

Upvotes: 1

Views: 1142

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80952

Try the following:

 get_Data() {
  return new Promise((resolve, reject) => {
     firebase.database().ref('transfer/').on('value', (snapshot) => {
     this.transfer = snapshot.val();
     resolve(snapshot.val());
     console.log('snapshot here',  this.transfer);
   });
  });
 }

Then inside your component, you can do:

 dataList(){
       this.authService.get_Data().then((value) => {
       console.log(value);
        });
    }

Check here for more info:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

Upvotes: 1

Related Questions