Should i initialize my const firebaseConfig = {};
for every js file?
CodePudding user response:
Nope, just once. If possible on the root js file
CodePudding user response:
You haven't mentioned the type of project you are working on.
Here is how it is done in React project
- Create
Firebase
folder undersrc
in React project - Create
config.js
inFirebase
folder
Initialize the app in config.js
and export it
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
// Replace the following with your app's Firebase project configuration
const firebaseConfig = {
apiKey: //your API key,
authDomain: "random-project.com"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
export {app, db}
// some other file
import { db } from '../Firebase/config';
const citiesCol = collection(db, 'cities');
const citySnapshot = await getDocs(citiesCol);
You can read in more depth in official docs here:https://firebase.google.com/docs/web/setup