Эффекты NgRx: Что я делаю неправильно? - PullRequest
0 голосов
/ 04 марта 2019

Поэтому я пытаюсь отправить различные массивы действий в зависимости от типа пользователя, который я получаю с сервера, когда я выполняю API-вызов /api/auth/login.

@Effect()
  login = this.actions$.pipe(
    ofType(AuthActions.ActionTypes.TryLogin),
    map((action: AuthActions.TryLogin) => {
      return action.payload;
    }),
    switchMap(loginData => {
      return this.httpClient.post<{
        jwtToken: string;
        usertype: string;
        expiresIn: number;
        userId: string;
      }>(`${this.BACKEND_URL}/api/auth/login`, loginData);
    }),
    mergeMap(res => {
      const actions = [
        {
          type: AuthActions.ActionTypes.Login
        },
        {
          type: AuthActions.ActionTypes.SetToken,
          payload: res.jwtToken
        },
        {
          type: AuthActions.ActionTypes.SetTokenExpiry,
          payload: res.expiresIn
        }
      ];
      if (res.usertype === 'Admin') {
        this.router.navigate(['/admin-panel']);

        return [...actions, {type: AdminActions.ActionTypes.SetId, payload: res.userId}];
      } else {
        this.router.navigate(['/client-panel']);

        return [...actions, {type: UserActions.ActionTypes.SetId, payload: res.userId}];

      }
    })
  );

Но я получаюэта ошибка:

Argument of type '(res: { jwtToken: string; usertype: string; expiresIn: number; userId: string; }) => ({ type: Act...' is not assignable to parameter of type '(value: { jwtToken: string; usertype: string; expiresIn: number; userId: string; }, index: number...'.
  Type '({ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: ...' is not assignable to type 'ObservableInput<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: strin...'.
    Type '({ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: ...' is not assignable to type 'ObservableInput<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: strin...'.
      Type '({ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: ...' is not assignable to type 'Iterable<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | ...'.
        Types of property '[Symbol.iterator]' are incompatible.
          Type '() => IterableIterator<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload...' is not assignable to type '() => Iterator<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string...'.
            Type 'IterableIterator<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: stri...' is not assignable to type 'Iterator<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | ...'.
              Types of property 'next' are incompatible.
                Type '{ (value?: any): IteratorResult<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes...' is not assignable to type '{ (value?: any): IteratorResult<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes...'. Two different types with this name exist, but they are unrelated.
                  Type 'IteratorResult<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string...' is not assignable to type 'IteratorResult<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string...'. Two different types with this name exist, but they are unrelated.
                    Type '{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: A...' is not assignable to type '{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: A...'. Two different types with this name exist, but they are unrelated.
                      Type '{ type: ActionTypes; payload: string; }' is not assignable to type '{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: A...'.
                        Type '{ type: ActionTypes; payload: string; }' is not assignable to type '{ type: ActionTypes; payload: string; }'. Two different types with this name exist, but they are unrelated.
                          Types of property 'type' are incompatible.
                            Type 'ActionTypes' is not assignable to type 'ActionTypes'. Two different types with this name exist, but they are unrelated.

Это работает нормально, если я разделяю логин администратора и логин пользователя.Но это будет означать, что большая часть кода будет идентична.Кто-нибудь может мне здесь помочь?

1 Ответ

0 голосов
/ 04 марта 2019

Я думаю, что единственный способ - это набрать как Action:

if (res.usertype === 'Admin') {
        this.router.navigate(['/admin-panel']);

        return [...actions, {type: AdminActions.ActionTypes.SetId, payload: res.userId} as Action];
} else {
        this.router.navigate(['/client-panel']);

        return [...actions, {type: UserActions.ActionTypes.SetId, payload: res.userId} as Action];
}

...