이 코드를 사용하여 데이터베이스에서 사용자 데이터를 가져옵니다.AngularFire 2 외부 데이터 액세스 .subscribe()
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase,AngularFireList} from 'angularfire2/database';
@Component({
selector: 'page-profile',
templateUrl: 'profile.html'
})
export class ProfilePage {
profileData: Observable<any[]>
constructor(private fire:AngularFireAuth,private db :AngularFireDatabase,public navCtrl: NavController) {
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.
});
console.log(this.profileData.username); // this is undefined
// How to access username outside subscribe function ?
}
}
내 질문은 : 어떻게 사용자 이름이 밖에 다른 곳에서 그것을 사용하는 기능을 구독 액세스 할 수? 코드에 주석을 추가했습니다. 이 같은
이 오래된 방법이 아닙니까? –