@Effect()中的服务未调用

时间:2019-08-24 14:56:49

标签: angular typescript redux rxjs ngrx

我有一种情况,其中调用了效果,但是未访问基础服务。是什么原因造成的?

user.effect.ts

@Effect()
getData$ = this.actions$.pipe(
    ofType(UserActions.getData),
    switchMap(() =>
        this.userService.getUserById(localStorage.getItem("uid")).pipe(
            map((data: IUser) => {
                if (data) {
                    return UserActions.dataReceived({
                        payload: UserService.parseData(data)
                    });
                } else {
                    return UserActions.dataNotReceived();
                }
            }),
            catchError(err =>
                of(
                    AuthActions.signOutError({
                        errorMessage: err.message,
                        errorCode: err.code
                    })
                )
            )
        )
    )
);

user.service.ts

getUserById(id) {
    this.logger.debug(`Getting user ${id}`);
    return this.afs
        .collection("users")
        .doc(id)
        .snapshotChanges()
        .pipe(
            map(action => {
                const data = action.payload.data();
                const uid = action.payload.id;
                return { uid, ...data };
            })
        );
}

1 个答案:

答案 0 :(得分:1)

您应该使用return new不返回。像这样

            if (data) {
                return new UserActions.dataReceived({
                    payload: UserService.parseData(data)
                });
            } else {
                return new UserActions.dataNotReceived();
            }