我在显示数据时做错了什么? - 离子 3 和火力地堡

What am I doing wrong in displaying data? - Ionic 3 & Firebase

我在 Ionic 3 应用程序中显示某些 Firebase 数据时遇到问题。这是我组件中的相关代码(abcdef 部分只是特定用户密钥的替代品):

var ref = firebase.database().ref('/profiles/abcdef/');
this.viewProfile = ref.once("value")
  .then(function(snapshot) {
    var firstName = snapshot.child("firstName").val();
    var lastName = snapshot.child("lastName").val();
    var orgName = snapshot.child("orgName").val();
    console.log(firstName+' '+lastName+' works at '+orgName );
  });

这是我在视图中看到的内容:

{{viewProfile.firstName}}

没有错误,只是没有任何显示。有什么想法吗?

您需要循环 snapshot 来获取数据

试试这个

var ref = firebase.database().ref('/profiles/abcdef/');
ref.once("value")
  .then(function(snapshot) {
      snapshot.forEach(function(data) {
          this.viewProfile = data.val();
      }
 });

你们非常亲密 @Hareesh:

var ref = firebase.database().ref('/profiles/abcdef/');
    ref.on('value' , profileSnapshot => {
              this.viewProfile = profileSnapshot.val();
     });

{{viewProfile.firstName}}

(不需要异步)似乎有效。

谢谢!!!