1
我在Angular中將@ ngrx/store和@ ngrx/effects與Firebase身份驗證一起使用。如何在ngrx/effects中使用承諾
這是構造:
constructor(
private actions: Actions,
private afAuth: AngularFireAuth,
private db: AngularFirestore,
private router: Router
) {}
這是效果:
@Effect()
getUser: Observable<Action> = this.actions
.ofType(authActions.GET_USER)
.map((action: authActions.GetUser) => action.payload)
.switchMap(payload => this.afAuth.authState)
.map(authData => {
if (authData) {
const user = new User(
authData.uid,
authData.displayName,
authData.email,
authData.photoURL
);
return new authActions.Authenticated(user);
} else {
return new authActions.NotAuthenticated();
}
})
.catch(err => Observable.of(new authActions.AuthError()));
此代碼按預期工作,但我需要的是存儲在公司的FireStore文檔中,我可以檢索其它值他們以這樣的方式
[...]
if (authData) {
const userRef = this.db.firestore
.collection('users')
.doc(authData.uid)
.get()
.then(doc => {
const userData = doc.data();
return {
uid: userData.uid,
displayName: userData.displayName,
email: userData.email,
photoURL: userData.photoURL,
role: userData.roles
};
});
[...]
但由於它是一個承諾,我不能使用userRef
我在哪裏使用user
。 有沒有解決這個問題的方法?