简体   繁体   English

用Angular获取firebase实时数据

[英]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.我正在将数据存储在 Firebase 实时数据库中,我正在尝试获取存储的数据,但似乎我没有正确获取它 - 我需要任何帮助。

auth.service.ts : auth.service.ts

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

returns回报

快照在这里

app.component.ts : app.component.ts

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

returning undefined ...返回undefined ...

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 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM