0
在我的項目中,我使用的是Ionic 3和AngularFire2。我必須從firebase數據庫中獲取價值並做一些更改並將其顯示在HTML頁面中,以便我必須等待訂閱完成。這裏是我的代碼:如何使用地圖代替Angularfire2中的訂閱,Ionic 3
items: FirebaseListObservable<any[]>;
constructor(public navCtrl: NavController, public afDB: AngularFireDatabase) {}
async ionViewDidEnter(){
var a = await this.calldatabase();
console.log(a); // it display first and value is undefined
console.log('3'); // it display second
}
async calldatabase(){
let category: any;
this.items = this.afDB.list('/cat', { preserveSnapshot: true });
await this.items.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log(snapshot.val()); // it displays third
category = snapshot.val();
});
});
return category;
}
`
我不能等待認購完成,然後我用Google搜索它,然後發現這些鏈接
angularfire2 wait for subscription to end?
Angular 2 Wait for subscription to complete
How to wait for a AngularFire2 subscription to finish before running the next lines of code Angular2
無法解決。現在,我試着去使用地圖,而不是認購不顯示任何
import 'rxjs/add/operator/map';
async calldatabase(){
let category: any;
this.items = this.afDB.list('/cat', { preserveSnapshot: true });
await this.items.map(snapshots => {
console.log(snapshots); // it displays nothing
snapshots.forEach(snapshot => {
console.log(snapshot.val()); // it displays nothing
category = snapshot.val();
});
});
return category;
}
我做到了,但它不工作 – LucidKit
也許你可以張貼到現在爲止已代碼。 – hsc