[英]How to assign value read from firebase to variable
I am trying to read the value from firebase and assign it to a global variable but it didn't work. 我正在尝试从firebase读取值并将其分配给全局变量,但是它不起作用。
public points: any;
readPoints(): any{
this.userProfile.child(this.user.uid).once('value').then((snapshot) =>{
this.points = snapshot.val().user_points;
});
}
console.log(this.points); //undefined
Users{
SAdS4cW57DSLuWr2obbZYUHsmAL2{
user_points: 20
.....
好吧,您的变量是this.points
但是您正在尝试打印this.point
I read this article from firebase and seem like solution is to call another function instead of assign value directly to variable 我从firebase阅读了这篇文章 ,似乎解决方案是调用另一个函数,而不是直接将值分配给变量
public points: any;
readPoints(): any{
this.userProfile.child(this.user.uid).once('value').then((snapshot) =>{
this.getPoints(snapshot.val().user_points);
});
}
getPoints(point: any = null): any{
this.points = parseInt(point) + 20;
console.log("point="+ this.points);
}
Your code seems right to me, try printing your snapshot.val()
to see what it gives you. 您的代码对我来说似乎正确,请尝试打印您的
snapshot.val()
看看它能为您带来什么。 I'll just add a code of how i imagine your full database query is. 我将添加一个代码,说明我如何想象您的完整数据库查询。
firebase.database().ref('Users/').child(this.user.uid).once('value').then(snapshot => {
this.points = snapshot.val().user_points;
})
Does it gives any error on the console? 它在控制台上是否给出任何错误? Is it getting the
this.user.id
right? 它正确地获得
this.user.id
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.