question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: inject() must be called from an injection context

See original GitHub issue

I am using @angular/cli@8.2.4, but the error happens as well in 8.0.1 (for me)

To reproduce it, I just had to follow the steps in “Getting started” section. After ng serve, the build runs well but the console outputs : Error: CollectionService requires AngularFirestore

But I am injecting AngularFirestoreModule in AppModule, and I imported it in the service as well. My code does not differ from documentation, that is :

import { Injectable } from '@angular/core';
import { MovieStore, MovieState } from './movie.store';
import { AngularFirestore } from '@angular/fire/firestore';
import { CollectionConfig, CollectionService } from 'akita-ng-fire';

@Injectable({ providedIn: 'root' })
@CollectionConfig({ path: 'movies' })
export class MovieService extends CollectionService<MovieState> {

  constructor(store: MovieStore) {
    super(store);
  }

}

I tried to inject AngularFirestore service in constructor too, but it was not better.

So, I installed the project from source to output the real error and it was Error: inject() must be called from an injection context Seems linked to this - still open - angular issue, but the proposed workaround does not work for me. I am having a hard time trying to understand this error.

EDIT: Here is how to easily reproduce it:

- ng new angular-test
- cd angular-test
- ng add @angular/fire
- ng add @datorama/akita
- ng g feature movies/movies
- (inject moviesService into app component constructor)
- ng serve

If necessary, I will add a stackblitz soon to reproduce this.

I was trying to do something similar to akita-ng-fire, but your project is way better. I would be glad to use it in our application, since you added atomic writings. This will be an essential tool for angularfire developers. Thumbs up 👍

NB: would be interesting to output the real error (or make a logger debug level) in addition to your error logs

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
loicmariecommented, Sep 6, 2019

I finally found out what happened, that was dumb. When using linked local library, we always put the library in an external project. But we found out in this issue that it is normally not working (don’t know why it worked until here with our other old librairies), and so the inject() method was not working.

I just put the npm linked librairies into the project/ directory and it’s working like a charm now, sorry for having bothered you. I can send PR to akita-ng-fire now 😃

0reactions
Sergey1997commented, Jan 25, 2021

@loicmarie haha, I had the same issue, thanks for your investigation. I just wasted about 4 hours to find the cause of issue…

Read more comments on GitHub >

github_iconTop Results From Across the Web

NG0203: `inject()` must be called from an injection context ...
You see this error when you try to use the inject() function outside of the allowed injection context. The injection context is available...
Read more >
inject() must be called from an injection context - Stack Overflow
There seems to be an issue when using npm link when consuming the library. Solution: use projects.$name.architect.build.options.
Read more >
Error: "inject() must be called from an injection context" after ...
After running ng serve the app crashes. See error message below. Exception or Error. Error: inject() must be called from an injection context...
Read more >
Error: inject() must be called from an injection context | Angular
Error : inject() must be called from an injection context. Hello, I am trying to use AngularJS Grid, but getting the error and...
Read more >
How do I fix "Inject() must be called from an injection context"?
I had to enable "preserveSymLinks" in the Angular. json of the main project. 18 }, Once that was in, I was able to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found