When I add provideEffects to my module, I encounter an error. However, when I remove it, the auth.effects.ts file doesn’t work as expected and fails to make API calls:
However I get this error:
at Function.defineProperty (<anonymous>)
at Module.createEffect (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/.angular/cache/18.2.4/vite/deps_ssr/@ngrx_effects.js:50:10)
at _AuthEffects.<instance_members_initializer> (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/src/app/auth/state/auth.effects.ts:13:12)
at new _AuthEffects (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/src/app/auth/state/auth.effects.ts:30:3)
at map (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/src/app/auth/state/auth.effects.ts:30:101)
at eval (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/.angular/cache/18.2.4/vite/deps_ssr/chunk-S4UJ36BC.js:4456:35)
at runInInjectorProfilerContext (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/.angular/cache/18.2.4/vite/deps_ssr/chunk-S4UJ36BC.js:3058:5)
at R3Injector.hydrate (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/.angular/cache/18.2.4/vite/deps_ssr/chunk-S4UJ36BC.js:4455:11)
at R3Injector.get (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/.angular/cache/18.2.4/vite/deps_ssr/chunk-S4UJ36BC.js:4345:23)
at injectInjectorOnly (/Users/stefanoantonetti/WebstormProjects/ngrx-counter-a17/.angular/cache/18.2.4/vite/deps_ssr/chunk-S4UJ36BC.js:3190:36
I’ve tried to remove the block
provideEffects(AuthEffects)
but then the Effects don’t work as expected and block API calls. Here’s my effects class:
import {Injectable} from "@angular/core";
import {Actions, createEffect, ofType} from "@ngrx/effects";
import {AuthService} from "../services/auth.service";
import {Router} from "@angular/router";
import {loginFailure, loginStart, loginSuccess} from "./auth.actions";
import {catchError, exhaustMap, map, of, tap} from "rxjs";
import {AuthResponse} from "../../models/auth-response.model";
@Injectable()
export class AuthEffects {
login$ = createEffect(() => {
return this.actions$?.pipe(
ofType(loginStart),
tap(action => console.log('Effetto login innescato', action)),
exhaustMap((action) =>
this.authService.login(action.email, action.password).pipe(
map((authResponse: AuthResponse) => {
console.log(authResponse)
this.router.navigate(['home']).then()
return loginSuccess()
}),
catchError(() => of(loginFailure()))
)
)
);
});
constructor(private actions$: Actions, private authService: AuthService, private router: Router) {}
}
This seems to indicate a potential issue with how the effects are registered or how the dependencies are provided in the module. I don’t really know what to do as seems to be a problem of misconfiguration of the file auth.effects.ts. I hope this piece of code I’m going to post might help(app.config.ts):
import {ApplicationConfig, isDevMode, provideZoneChangeDetection} from "@angular/core";
import {provideRouter} from "@angular/router";
import {routes} from "./app.routes";
import {provideClientHydration} from "@angular/platform-browser";
import {provideHttpClient, withFetch} from "@angular/common/http";
import {provideStore} from "@ngrx/store";
import {counterReducer} from "./counter/state/counter.reducer";
import {postsReducer} from "./posts/state/posts.reducer";
import {authReducer} from "./auth/state/auth.reducer";
import {provideEffects} from "@ngrx/effects";
import {AuthEffects} from "./auth/state/auth.effects";
import {provideStoreDevtools} from "@ngrx/store-devtools";
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes), provideClientHydration(),
provideHttpClient(withFetch()),
provideStore({
counter: counterReducer,
posts: postsReducer,
auth: authReducer,
}),
provideEffects(AuthEffects),
provideStoreDevtools({ maxAge: 25, logOnly: !isDevMode() })
]
};
Stefano Antonetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.