angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.64k stars 2.2k forks source link

AFS 6.1.0 : Error: Firestore has already been started and its settings can no longer be changed. #2666

Closed glued closed 3 years ago

glued commented 3 years ago

After upgrading our project to 6.1.0 we started seeing this error

Version info

Angular: 11.0.1

Firebase: 8.0.2

AngularFire: 6.1.0

How to reproduce these conditions

import { AngularFirestoreModule, SETTINGS } from '@angular/fire/firestore';

@NgModule({
  imports: [
    AngularFirestoreModule,
    AngularFireAuthModule,
    AngularFireStorageModule,
    AngularFireModule.initializeApp(environment.firebase),
 ],
 providers: [
 {
   provide: SETTINGS,
   useValue: { ignoreUndefinedProperties: true, cacheSizeBytes: 1048576 },
 };
]

removing settings fixes the issue...

Debug output

Errors in the JavaScript console

FirebaseError: Firestore has already been started and its settings can no longer be changed. You can only modify settings before calling any other methods on a Firestore object.

Expected behavior

no errors

jamesdaniels commented 3 years ago

I'm unable to reproduce, are you using HMR?

glued commented 3 years ago

no HMR

athoma13 commented 3 years ago

Haha, I have been creating a repro for this the last 3 hours, to find a bug raised 2 hours ago...

The way the issue manifests itself for me is when I use a combination of Lazy-Loaded Routes AND providing injection token SETTINGS. In the component from the lazy-loaded route, the (new injected instance of) AngularFirestore is trying to apply settings again to the firebase object (see constructor of AngularFirestore.ts).

In the repro, using the following versions yields the expected result.

But breaks when using the following versions (and beyond)

IMHO, the AngularFirestore should be a singleton service.

Here is a repro: https://stackblitz.com/edit/firestore-lazy-module-bug-repro?devtoolsheight=33&file=readme.md

Hope this helps...

fr-esco commented 3 years ago

I have the same problem on Angular 10.2.x and Angular Fire 6.0.x.

The scenario described by @athoma13 raises the same issue on my project.

Maybe, AngularFirestore can be providedIn: 'root' or provided by AngularFirestoreModule. (code ref)

jamesdaniels commented 3 years ago

Addressed in #2667, this was the intent of my instance cache but I had an implementation error. 6.1.1 is being cut now with a fix.

fr-esco commented 3 years ago

@jamesdaniels May you backport it to 6.0.x, please?