我来自python / golang背景,现在我潜入了ionic2。 我想知道我是做蠢事,还是有问题,根据我目前对堆栈的了解,我无法分辨它的来源。也许我只需要一种方法来引用这个函数的外部范围。好吧,我正在遍历我的用户集合并将数据打印到控制台,如下所示,代码库位于问题的下方:
page1.ts:81 key -KFoJ-oF-ll04zmxJZiL
page1.ts:88 data Object {email: "sdcsd@gmail.com", username: "cesscd"}
page1.ts:89 user UserAllowed {email: "sdcsd@gmail.com", username: "cesscd"}
但是一旦我尝试将新用户推送到允许的用户数组,我就会收到此错误;
FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property 'alloweduser' of undefined
at http://localhost:8100/build/js/app.bundle.js:98:21
at https://cdn.firebase.com/js/client/2.4.1/firebase.js:200:356
at Kc.h.ka (https://cdn.firebase.com/js/client/2.4.1/firebase.js:35:275)
at Kc.h.ka (https://cdn.firebase.com/js/client/2.4.1/firebase.js:35:268)
at Kc.h.ka (https://cdn.firebase.com/js/client/2.4.1/firebase.js:35:268)
at Kc.h.ka (https://cdn.firebase.com/js/client/2.4.1/firebase.js:35:268)
at Ec.h.ka (https://cdn.firebase.com/js/client/2.4.1/firebase.js:32:465)
at fe.h.R (https://cdn.firebase.com/js/client/2.4.1/firebase.js:83:379)
at W.forEach (https://cdn.firebase.com/js/client/2.4.1/firebase.js:200:326)
at http://localhost:8100/build/js/app.bundle.js:88:22
export class Page1 {
reponse:string;
firebaseUrl:string;
userRef:Firebase;
alloweduser: UserAllowed[];
constructor() {
this.firebaseUrl = "https://fghghgf-hefghgfhat-gh.firebaseio.com/web/data/";
this.userRef = new Firebase(this.firebaseUrl).child("users");
}
ngOnInit(){
this.alloweduser=[];
this.userRef.once("value", function(snapshot) {
// The callback function will get called twice, once for "fred" and once for "barney"
snapshot.forEach(function(childSnapshot) {
// key will be "fred" the first time and "barney" the second time
var key = childSnapshot.key();
console.log("key",key);
// childData will be the actual contents of the child
var childData = childSnapshot.val();
var user = new UserAllowed("","");
var user = new UserAllowed(childData.username,childData.email);
console.log("data",childData);
console.log("user",user);
//Why this.alloweduser get undefined inside this function?
this.alloweduser.push(user);
});
});
}
}
// TypeScript
class UserAllowed {
// Property (public by default)
email: string;
username: string;
// Constructor
// (accepts a value so you can initialize engine)
constructor(username:string, email: string) {
this.email = email;
this.username = username;
}
}
答案 0 :(得分:1)
你在回调函数中使用this
,它具有不同的含义。一种解决方案是使用fat-arrow / rocket符号进行回调,确保this
符合您的预期:
this.userRef.once("value", (snapshot) => {
// The callback function will get called twice, once for "fred" and once for "barney"
snapshot.forEach((childSnapshot) => {
// key will be "fred" the first time and "barney" the second time
var key = childSnapshot.key();
console.log("key",key);
// childData will be the actual contents of the child
var childData = childSnapshot.val();
var user = new UserAllowed("","");
var user = new UserAllowed(childData.username,childData.email);
console.log("data",childData);
console.log("user",user);
//Why this.alloweduser get undefined inside this function?
this.alloweduser.push(user);
});
});
有关this
含义的更多信息,请参阅How to access the correct `this` context inside a callback?