나는 firebase에서 한 번만 데이터를 가져 오려고합니다. 이 코드를 사용하여 실시간으로 변경 사항 및 업데이트를 구독하고 감시합니다.AngularFire 2 v5 데이터를 한 번만 가져 오는 방법?
this.profileData = this.fire.authState.switchMap(auth => this.db.object(`profile/${auth.uid}`).snapshotChanges().map(action => {
const $key = action.payload.key;
const data = { $key, ...action.payload.val() };
return data;
})).subscribe(profile => {
this.profileData = profile;
console.log(this.profileData.username); // this is working.
});
나는 이런 식으로 뭔가를 시도했지만, 당신은 당신이 구독 취소에 대해 걱정해야 자신의 관찰 가능한을 사용하는 경우는이
this.profileData = this.fire.authState.switchMap(auth => this.db.object(`profile/${auth.uid}`)).take(1).subscribe(profile =>{
console.log(profile.username);
}));
'take (1)'이 작동해야합니다. [구독하지 않고 한 번만 값을 얻으십시오.] (https://github.com/angular/angularfire2/issues/456 # issuecomment-241509299). – Grimthorr
this.profileData = this.fire.authState.switchMap (auth => this.db.object ('profile/$ {auth.uid}')). console.log (this.profileData.username); 이 정의되지 않은 반환합니까? –
with console.log (this.profileData) 이걸 가지고 있습니다 : http://prntscr.com/h0waiy –