angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AuthModule)[AuthService -> AuthService -> AngularFirestore -> InjectionToken

Open ashikjs opened this issue 4 years ago • 13 comments

Angular: 13.0.3

Firebase: 9.4.0

AngularFire: 7.2.0

After upgrade angularfire 6 to 7 i got error

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AuthModule)[AuthService -> AuthService -> AngularFirestore -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options]

simple if i can it any component constructor

private firebaseAuth: AngularFireAuth, or

private angularFirestore: AngularFirestore,

ashikjs avatar Dec 02 '21 01:12 ashikjs

This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Dec 02 '21 01:12 google-oss-bot

I see this pretty chaotic. The samples are so out of date and one cannot really follow them. I am getting the same errors.

I was able to initialize my module like this, and I try to stay our of compact/ package as it gives tons of errors.

import {initializeApp, provideFirebaseApp} from '@angular/fire/app';
import {connectFirestoreEmulator, getFirestore, provideFirestore} from '@angular/fire/firestore';
import {connectStorageEmulator, getStorage, provideStorage} from '@angular/fire/storage';
import {Auth, connectAuthEmulator, getAuth, provideAuth} from '@angular/fire/auth';


provideAuth(() => {
      const auth = getAuth();
      if (environment.useEmulators) {
        connectAuthEmulator(auth, 'http://localhost:9099', {disableWarnings: true});
      }
      auth.useDeviceLanguage();
      setPersistence(auth, indexedDBLocalPersistence);
      return auth;
    }),

I am not really sure which API or which documentation should I follow

https://github.com/angular/angularfire/blob/master/site/src/auth/route-guards.md or https://github.com/angular/angularfire/blob/master/docs/auth/router-guards.md

If I use the one from /site/ then it is also out of sync with latest code as this import is invalid.

import { AngularFireAuthGuard } from '@angular/fire/auth-guard';

fkolar avatar Dec 02 '21 14:12 fkolar

Same here! I use a config generated by @angular/fire schematic:

  imports: [
    //....
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideAuth(() => getAuth()),
    provideFirestore(() => getFirestore()),
  ],

I get the same error

DMezhenskyi avatar Dec 05 '21 16:12 DMezhenskyi

as For teh AuthGuard, I had copy the class code to my local class to make it work. Due the to export and canActivate signture problems.

fkolar avatar Dec 06 '21 22:12 fkolar

Same here! I use a config generated by @angular/fire schematic:

  imports: [
    //....
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideAuth(() => getAuth()),
    provideFirestore(() => getFirestore()),
  ],

I get the same error

For compat imports you need to import the 'old' modules. In your case: AngularFireModule.initializeApp(environment.firebase).

blaiseAI avatar Feb 01 '22 19:02 blaiseAI

same here

PJozeph avatar Feb 02 '22 13:02 PJozeph

You need to import the old module as a work around, "AngularFireModule.initializeApp(environment.firebase)"

cashco-bsebagabo avatar Feb 02 '22 14:02 cashco-bsebagabo

you need to declare the providers :

@NgModule({
  declarations: [
    LoginComponent
  ],
  imports: [
    CommonModule,
    AuthRoutingModule,
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideAuth(() => getAuth()),
  ],
  providers : [
    AuthService
  ]
})
export class AuthModule { }

although my AuthService has the following meta data

@Injectable({
  providedIn: 'root'
})

I dont know why its is not working

PJozeph avatar Feb 03 '22 08:02 PJozeph

I had similar but different null injector error: core.mjs:6485 ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(... ) -> FirestoreService -> ka -> ka -> ka. This was from importing Firestore from firebase instead of angular/fire. Created an issue here to help other find. It was kind of stupid mistake but easy to make when letting IDE make imports, and really hard to debug. Hope this saves someone else some lost hours.

nayfin avatar Feb 09 '22 16:02 nayfin

Also got the error. Pretty new to Angular so don't know how it happened, but why is this issue even existing? Are the schematics broken/buggy?

mystiquewolf avatar Jun 30 '22 17:06 mystiquewolf

I'm having a similar issue, but probably PEBKAC in some way. https://github.com/BrightChain/quri-dev/runs/7417287755?check_suite_focus=true#step:7:161

JessicaMulein avatar Jul 19 '22 20:07 JessicaMulein

Hi! This works for me. I put this in app.module.ts

import { FIREBASE_OPTIONS } from '@angular/fire/compat';

@NgModule({ ... providers: [ { provide: FIREBASE_OPTIONS, useValue: environment.firebaseConfig } ], })

simonbarbascu avatar Jul 26 '22 10:07 simonbarbascu

Hi, I had same problem, I solved adding AngularFireModule.initializeApp(environment.firebaseConfig) into imports of AppModule

import { AngularFireModule } from '@angular/fire/compat'; import { environment } from 'src/environments/environment';

@NgModule({ declarations: [AppComponent], imports: [ AppRoutingModule, BrowserModule, AngularFireModule.initializeApp(environment.firebaseConfig), ], providers: [...], bootstrap: [AppComponent], })

Milly00 avatar Sep 17 '22 00:09 Milly00

Hi, I had same problem, I solved adding AngularFireModule.initializeApp(environment.firebaseConfig) into imports of AppModule

import { AngularFireModule } from '@angular/fire/compat'; import { environment } from 'src/environments/environment';

@NgModule({ declarations: [AppComponent], imports: [ AppRoutingModule, BrowserModule, AngularFireModule.initializeApp(environment.firebaseConfig), ], providers: [...], bootstrap: [AppComponent], })

This worked beautifully, thanks!

qaderHaddad avatar Oct 07 '22 18:10 qaderHaddad

I am having the same problem and the answers provided above leave me with a red line under firebaseConfig.

error is saying 'Property 'firebaseConfig' does not exist on type '{ production: boolean; }'.'

ruthmazango avatar Feb 10 '23 16:02 ruthmazango

Hi @ruthmazango, You have the firebaseConfig in environment.prod.ts ? Check the name with which you added the firebase configuration. In my case, I have it as: export const environment = { .... firebaseConfig: **Your config of firebase** .... } The same configuration that you have in environment.ts must be in environment.prod.ts

I hope it helps!

Milly00 avatar Feb 10 '23 17:02 Milly00

thanks, it worked now

ruthmazango avatar Feb 13 '23 10:02 ruthmazango

Hi! This works for me. I put this in app.module.ts

import { FIREBASE_OPTIONS } from '@angular/fire/compat';

@NgModule({ ... providers: [ { provide: FIREBASE_OPTIONS, useValue: environment.firebaseConfig } ], })

This works!! thanks!!! <3

laladiaz avatar Mar 09 '23 22:03 laladiaz

I had this error when I was using the v9 syntax to set up firebase but had an import using the compat directories.

If you are having the same issue I recommend the following:

If you are using the V9 sytanx (provideFirebaseApp(() => initializeApp(environment.firebase)), in your app.module.ts) then search your codebase and look for any imports using compat. In my case it was the auth guard which was importing from compat/auth-guard. Remove the compat so the imports become import { canActivate, redirectLoggedInTo } from '@angular/fire/auth-guard';

Do not mix V9 and compat!

kisamoto avatar Mar 21 '23 08:03 kisamoto