Typescript Angular NGRX/Effects actions$.pipe() undefined
Asked Answered
B

3

7

Hello I have issue with ngrx/effects - pipe undefined. Below I attached sample code which is correct along compilator but browser shows undefined pipe error.

constructor(
    private actions$: Actions,
    private ethereumService: EthereumService
) { }

loadUser$ = createEffect(() =>
    this.actions$.pipe(
        ofType(loadAccountState),
        mergeMap(() => this.ethereumService.getAccountDetails()
            .pipe(
                map(setAccountStateCompleted),
                catchError(() => EMPTY)
            )
        )
    )
);

AppModule:

StoreModule.forRoot(reducers),
EffectsModule.forRoot([AccountEffects]),

EDIT: Even this sample has same error -_-

logActions$ = createEffect(() =>
    this.actions$.pipe(
        ofType(AccountActions.loadAccountState),
        tap(action => console.log(action))
    ), { dispatch: false });

PS2. I am using ActionReducerMap which is main reducer file imported to Root as reducers

import {
  createSelector,
  createFeatureSelector,
  ActionReducerMap,
} from '@ngrx/store';

import * as fromAccount from './account.reducer';

export interface State {
  account: fromAccount.State;
}

export const reducers: ActionReducerMap<State> = {
  account: fromAccount.updateAccountReducer,
};

export const selectAccountState = createFeatureSelector<fromAccount.State>('account');

//Account Selectors
export const selectCurrentUser = createSelector(
  selectAccountState,
  fromAccount.selectActiveAccount
);

What is wrong with my code, please for help

Bechler answered 18/4, 2022 at 19:14 Comment(5)
did you inject actions$ in your constructor ?Rivet
Edited, yes I declared actions constructor.Bechler
Extended information added.Bechler
I moved effects to constructor and it works. I have to moce all code to strict mode.Bechler
I don't understand why, (maybe any change in versions) because my code was working but moving effect inside constructor as @UlandNimblehoof suggested, it is working. :OVolume
E
17

Depending how the $actions is declared the generated code might change when you set target ES2022 or newer.

Quick fix - add this to your tsconfig.json:

"compilerOptions": {
  {
    // ...
    "useDefineForClassFields": false
    // ...
  }
}

For future you might want to refactor class initialization and avoid this flag.

More info:

This issue can also happen when switching compiler target to ES2022 and some Jest tests fail just because of that.

Epimorphosis answered 18/1, 2023 at 9:56 Comment(3)
this did the trick for me!Lynching
I spent quite a bit of time on this, and this is the only thing that worked. Thank you so much. (using Angular 15.2 and es2022)Stickpin
This should be the correct answer if using Angular with ES2022Scamander
L
4

In angular 15, use

actions$ = inject(Actions);

when the lib is set to ES2022 in your tsconfig

Lynettelynn answered 12/10, 2023 at 2:52 Comment(0)
H
1

I fixed this problem by updating my tsconfig.json file to reference ES2020 instead of ESNEXT.
tsconfig keys

After this update, in my effects file, I have my createEffect methods outside the constructor and it's working for me.

Hercules answered 4/10, 2022 at 23:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.