-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Cannot read properties of undefined (reading 'name') at isClassInstance" when importing effects from a library #3972
Comments
Hi @mnlttt, I couldn't reproduce this behavior. Can you provide reproduction via Stackblitz playground or GitHub repo? |
@markostanimirovic I don't have a public published library I'm sorry, but I can give you more details about how to reproduce it: Using Nx I built a buildable and publishable library where I had this: // my-effects.effects.ts
export const myEffect = createEffect(
[...],
{ functional: true },
); import * as MyEffects from './my-effects.effects';
export const myStoreProvider: () => EnvironmentProviders = () =>
makeEnvironmentProviders([
provideEffects(MyEffects),
]);
export const myProvider = (): EnvironmentProviders =>
makeEnvironmentProviders([
myStoreProvider(),
]); Then, in my App, I imported providers: [
myProvider(),
], BUT, if I remove export const myProvider = (): EnvironmentProviders =>
makeEnvironmentProviders([
]); providers: [
myProvider(),
myStoreProvider(),
], The thing that must happen to get the error is to build the library and install it ( |
Thanks for more info! But as we mentioned in the bug report template, reproduction via GitHub repo/StackBlitz playground is required, especially for more complex scenarios. Feel free to create an Nx repo that reproduces mentioned behavior, share the link here, and we'll take a look. Maybe the problem is not related to NgRx at all. It's weird that an object with effects (or any other object) doesn't have a |
@markostanimirovic there you go https://github.com/mnlttt/ngrx-app It already conveniently has the library built, in the root. The lib is this https://github.com/mnlttt/ngrx-lib I tried running the app on StackBlitz but I get this error:
|
Published the package on npmjs and made the app without Nx. https://stackblitz.com/github/mnlttt/ngrx-app-vanilla?file=src%2Fmain.ts IMPORTANT: add |
Thanks @mnlttt! I'll take a look. |
I have the same issue.
After building an Angular library that uses effects, i have the same error in the console at line ngrx-effects.mjs:134. The error is that in the function ERROR TypeError: Cannot read properties of undefined (reading 'name')
at isClassInstance (ngrx-effects.mjs:134:29)
at ngrx-effects.mjs:322:55
at groupBy.js:25:29
at OperatorSubscriber._next (OperatorSubscriber.js:13:21)
at OperatorSubscriber.next (Subscriber.js:31:18)
at Subject.js:34:30
at errorContext (errorContext.js:19:9)
at EffectSources.next (Subject.js:27:21)
at EffectSources.addEffects (ngrx-effects.mjs:316:14)
at useValue (ngrx-effects.mjs:676:35) On the other hand, when running the library from source code (without building), everything works fine. |
Avoiding the |
Agree 👍 Btw, the issue exists because const usersEffects = Object.freeze({
__proto__: null,
// ... functional effects
}) |
As pointed above #3972 (comment), avoiding the Here is my current solution: import { inject } from '@angular/core';
import { Actions, createEffect, FunctionalEffect } from '@ngrx/effects';
// Do not export each effect
const myEffect$ = createEffect(
(actions$ = inject(Actions)) => {
return actions$.pipe(/* ... */);
},
{ functional: true }
);
// Just add this
export const myFeatureEffects: Record<string, FunctionalEffect> = { myEffect$ }; Then, use your effects as usual: import { myFeatureEffects } from './my-feature.effects.ts';
provideEffects(myFeatureEffects); |
For those who still have to use version below 16.2.0 for some reason, you can use this as a quick fix: import * as MyEffects from './my-effects.effects';
export const myStoreProvider: () => EnvironmentProviders = () =>
makeEnvironmentProviders([
provideEffects({ ...MyEffects }),
]); Spread operator creates a new object which does have a proper constructor and problem is resolved. |
Which @ngrx/* package(s) are the source of the bug?
effects
Minimal reproduction of the bug/regression with instructions
at isClassInstance (ngrx-effects.mjs:135:29)
Minimal reproduction of the bug/regression with instructions
Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)
NgRx: ^16.1.0
Angular: ^16.1.7
Node: 18.17.0
Other information
No response
I would be willing to submit a PR to fix this issue
The text was updated successfully, but these errors were encountered: