Adi*_*ari 5 javascript firebase ionic-framework ionic2 angularfire2
我正在尝试使用AngularFire2从我的Firebase获取数据.我想查看具体数据,在从Firebase获取此数据后,我只能在特定范围内检查,而不是在对Firebase进行操作后检查.为什么会这样?
以下是我的代码:
this.af.database.list('/users/1qfcMAQnglX9jsW5GdLpPko1HqE2', { preserveSnapshot: true})
.subscribe(snapshots=>{
snapshots.forEach(snapshot => {
if(snapshot.key=="reg_boolean"){
console.log(snapshot.val());
this.bo=snapshot.val();
}
this.currentUser.push({key:snapshot.key,value:snapshot.val()});
console.log(this.currentUser);
//console.log(snapshot.key, snapshot.val());
if(this.bo==true){console.log("happy"); }; //i can access only in this scope
});
})
if(this.bo==true){console.log("happy"); }; //why i can access this value??it's undefined, this happen before the subscribe with angularfire2
Run Code Online (Sandbox Code Playgroud)
它是未定义的,因为this.af.database.list它是异步的,因此代码subscribe将在this.af.database.list检索数据时执行.因此,当代码到达时,if(this.bo==true){console.log("happy"); };它没有任何东西,因为订阅根本没有完成.
订阅它就像旧的承诺,但现在它正在使用rxjs我建议你学习它因为角度和离子有很多关注.
| 归档时间: |
|
| 查看次数: |
496 次 |
| 最近记录: |