2017-11-18 118 views
1

我在我的Ionic 3应用程序中显示一些Firebase数据时遇到问题。下面是我的组件相关的代码(ABCDEF部分只是针对特定用户的主要替补):我在做什么错误显示数据? - Ionic 3&Firebase

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}} 

没有错误,根本不算什么显示器。有任何想法吗?

+0

您是否检查控制台?名字是否打印? –

+0

尝试添加异步{{viewProfile.firstName | async}} –

+0

是的,我应该提到 - 控制台确实显示了所有的变量,但我无法在页面模板本身中正确显示。不幸的是,添加异步并没有解决这个问题。 – Nick

回答

0

你需要循环的snapshot获取数据

试试这个

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

事实上,snapshot.child(“firstName”).val()确实可以在控制台中显示每个变量,就像您的解决方案一样。但是,它们都不能在页面模板中显示数据。 – Nick

+0

你需要'async'管道。请显示代码如何完成。 – Hareesh

+0

@PrithiviRaj建议它 – Hareesh

1

你非常接近@Hareesh:

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

{{viewProfile.firstName}} 

(不需要异步)似乎工作。

谢谢!!!

相关问题