【发布时间】:2017-08-04 04:35:24
【问题描述】:
我在 Firebase 数据库中使用 Angular 4 和 angularfire。我有以下 Firebase 数据库
"questions" : {
"tv" : {
"got" : {
"1" : {
"answer1" : "Death",
"choice11" : "Exile",
"choice12" : "You lose a hand",
"choice13" : "You lose the privilege to marry and father children",
"description" : "",
"q1title" : "What is the punishment for deserting the Night’s Watch?"
},
"questions" : {
"number" : 1
}
}
}
}
我想读取按钮上的问题编号并将其传递给我的程序以在函数等上使用它。我目前正在使用这部分代码来读取它
constructor(public afAuth: AngularFireAuth, public af: AngularFireDatabase,public authService: AuthService,
private router: Router) {
var ref = firebase.database().ref("questions/tv/got/questions");
ref.once("value")
.then(function(snapshot) {
let qnumber = snapshot.child("number").val();
console.log(qnumber);
});
}
一切正常,我可以在 concole 上看到 qnumber(qnumber=1)。但是我无法在程序上传递 qnumber 并将其用于我的其他功能 我试图声明第二个变量,并像这样给它 qnumber 值
qnumber2;
constructor(public afAuth: AngularFireAuth, public af: AngularFireDatabase,public authService: AuthService,
private router: Router) {
var ref = firebase.database().ref("questions/tv/got/questions");
ref.once("value")
.then(function(snapshot) {
let qnumber = snapshot.child("number").val();
this.qnumber2 = qnumber;
console.log(qnumber2);
});
但是我在编译器上遇到错误。您能帮我告诉我一种将程序中的 qnumber 作为整数传递的方法吗?
【问题讨论】:
标签: javascript angular typescript firebase firebase-realtime-database