내 프로젝트에서는 Ionic 3과 AngularFire2를 사용하고 있습니다. firebase 데이터베이스에서 값을 가져 와서 일부 변경 작업을 수행하고 HTML 페이지에 표시해야 구독이 완료 될 때까지 기다려야합니다. 여기 내 코드입니다 :Angularfire2, Ionic 3에서 Subscription 대신 Map을 사용하는 방법
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;
}
`
내가 가입이 완료 될 때까지 기다립니다 수 없습니다, 그때 나는 그것을 봤 후 다음 링크를
Angular 2 Wait for subscription to complete
angularfire2 wait for subscription to end?
발견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