After delving into the process of connecting your app to the Firebase emulators like Firestore emulator, I came across the primary documentation which outlined the steps for Web version 9:
import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";
// firebaseApps previously initialized using initializeApp()
const db = getFirestore(); // <-- Notably, no passing of firebaseApp here
connectFirestoreEmulator(db, 'localhost', 8080);
I've also come across an alternative way to call getFirestore()
:
import { initializeApp } from 'firebase/app';
import { getFirestore, connectFirestoreEmulator } from 'firebase/firestore';
const config = { /* Firebase project config here */ };
const firebaseApp = initializeApp(config);
const db = getFirestore(firebaseApp); // <-- Note that firebaseApp is passed-in
connectFirestoreEmulator(db, 'localhost', 8080);
In the Firestore API docs for getFirestore(), it mentions that:
"Returns the existing Firestore instance that is associated with the provided FirebaseApp. If no instance exists, initializes a new instance with default settings."
This has left me puzzled about whether I should pass in my initialized firebaseApp
when calling
getFirestore()</code based on that description. Since I have multiple Firebase services to emulate and have them communicate with each other, I believe I <em>should</em> provide my <code>firebaseApp
.
Which approach is correct? Are there any potential pitfalls I should be aware of?