Firebase 3 - Ionic 2 - .on('value' , snapshot => { this...} this is null
Firebase 3 - Ionic 2 - .on('value' , snapshot => { this...} this is null
当我尝试使用 Firebase 3 SDK for Web 检索数据时,"this" 显示为空。知道为什么会发生这种情况吗?
this.mainApp = firebase.initializeApp(config);
FBGet(path){
return this.mainApp.database().ref(path);
}
...
GetUser(){
this.appData.FBGet('users/'+user.uid).on('value', function(snapshot) {
var objUser = snapshot.val();
//the following statement is throwing an exception cause 'this' is null
this.events.publish('user:update', objUser);
});
}
谢谢
您没有为 FBGet
使用箭头函数,因此 this
不会绑定到您的 component/provider。
GetUser(){
this.appData.FBGet('users/'+user.uid).on('value', (snapshot) => {
const objUser = snapshot.val();
// this is now bound to the component using the fat arrow =>
this.events.publish('user:update', objUser);
});
}
以上解决方案仅适用于 如果 this.events
是同一个 component/provider 上的 属性。
当我尝试使用 Firebase 3 SDK for Web 检索数据时,"this" 显示为空。知道为什么会发生这种情况吗?
this.mainApp = firebase.initializeApp(config);
FBGet(path){
return this.mainApp.database().ref(path);
}
...
GetUser(){
this.appData.FBGet('users/'+user.uid).on('value', function(snapshot) {
var objUser = snapshot.val();
//the following statement is throwing an exception cause 'this' is null
this.events.publish('user:update', objUser);
});
}
谢谢
您没有为 FBGet
使用箭头函数,因此 this
不会绑定到您的 component/provider。
GetUser(){
this.appData.FBGet('users/'+user.uid).on('value', (snapshot) => {
const objUser = snapshot.val();
// this is now bound to the component using the fat arrow =>
this.events.publish('user:update', objUser);
});
}
以上解决方案仅适用于 如果 this.events
是同一个 component/provider 上的 属性。