我在做什么错误显示数据? - Ionic 3&Firebase
问题描述:
我在我的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}}
没有错误,根本不算什么显示器。有任何想法吗?
答
你需要循环的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}}
(不需要异步)似乎工作。
谢谢!!!
您是否检查控制台?名字是否打印? –
尝试添加异步{{viewProfile.firstName | async}} –
是的,我应该提到 - 控制台确实显示了所有的变量,但我无法在页面模板本身中正确显示。不幸的是,添加异步并没有解决这个问题。 – Nick