无法在Firebase上设置null的属性“variable” [英] Cannot set property 'variable' of null on Firebase
问题描述
我有这段代码来检查连接到互联网的Firebase。但问题表明我的 this.total_downloaded = 1
无法设置,即使我在 construct()
之前声明为 public total_downloaded = 0
。
I have this code to check either the Firebase connected to the internet or not. But the problem shows that my this.total_downloaded=1
cannot be set even I was declared before construct()
as public total_downloaded=0
.
为什么会这样?我不应该能够轻松使用我的功能或设置变量吗?
Why does it happen like that? Shouldn't I be able to use my function or set the variable easily?
有人能帮帮我吗?谢谢。这是我的代码:
Can someone help me? Thanks. Here's my code:
public this.total_downloaded = 0;
//...
var connectedRef = firebase.database().ref('/.info/connected');
connectedRef.once("value", function(snap) {
if (snap.val() === true) {
firebase.database()
.ref("last_update")
.once('value', (snap_0) => {
if (data.rows.item(0).value != snap_0.val().value) {
this.update_hok_baghu(db);
var query_insert_last_update = "UPDATE last_update SET value =" + snap_0.val().value + "";
db.executeSql(query_insert_last_update, []).then(() => {
}, (error) => {
console.log("ERROR on update to last_update: " + JSON.stringify(error));
});
} else {
this.total_downloaded = 1;
}
});
} else {
this.total_downloaded = 1;
}
});
推荐答案
您正在使用常规函数作为回调功能(SNAP)
。
所以 else
条件中的 this
指的是回调函数,而不是你的类。
You are using the regular function as callback function(snap)
.
So the this
in your else
condition refers to the callback function and not your class.
使用箭头功能:
connectedRef.once("value", (snap)=> {
if (snap.val() === true) {
firebase.database()
.ref("last_update")
.once('value', (snap_0) => {
if (data.rows.item(0).value != snap_0.val().value) {
this.update_hok_baghu(db);
var query_insert_last_update = "UPDATE last_update SET value =" + snap_0.val().value + "";
db.executeSql(query_insert_last_update, []).then(() => {
}, (error) => {
console.log("ERROR on update to last_update: " + JSON.stringify(error));
});
} else {
this.total_downloaded = 1;
}
});
} else {
this.total_downloaded = 1;//this will refer to class now.
}
});
这篇关于无法在Firebase上设置null的属性“variable”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!