0
我有一个使用Angular 4,ngrx和AngularFire2构建的项目。我正在尝试检索当前登录用户的uid并将其放入我的商店。AngularFire2 return可观察到ngrx效果
任何想法或援助,将不胜感激。
错误
ERROR TypeError: Cannot read property 'map' of undefined
at SwitchMapSubscriber.project (auth.effects.ts:22)
影响代码
@Effect() getAccountId$ = this.actions$
.ofType(ActionTypes.GET_ACCOUNT_ID)
.map(action =>
// This is line 22 where the error is coming
this.authService.getCurrentUserId()
.map(authObject => ({ type: ActionTypes.GET_ACCOUNT_ID_SUCCESS, payload: authObject }))
.catch(() => Observable.of({ type: ActionTypes.GET_ACCOUNT_ID_ERROR })));
服务代码
用户名:任何; //声明为任何。如果我尝试使其成为Observable,那么我会在this.userId = user.uid上得到一个编译错误;在调用afAuth说,它不能指定字符串可观察
userId: any;
getCurrentUserId(): Observable<any> {
this.afAuth.authState.subscribe(
(user: firebase.User) => {
if (user != null) {
this.userId = user.uid;
console.log('constructor auth object: ', user);
console.log('constructor userId: ', this.userId);
}
});
return this.userId;
}
如果在'.ofType(ActionTypes.GET_ACCOUNT_ID)'行之前和之后执行'.do(res => console.log(res)),你会得到什么? – crash
我不认为你的'this.userId'是一个'Observable'。 –
嗨感谢您的快速回复。我不能把它放在那行前,因为我得到一个编译错误,但在我得到对象{类型:“GET_ACCOUNT_ID”,有效载荷:“”}。我已经确定了问题所在,只是不确定如何解决问题。基本上,对auth对象的调用是一个subsciption,所以它在订阅完成之前返回this.userId效果,所以它返回undefined – ccocker