skip to Main Content

This is a follow up question to my post here –>
Firebase web modular API get() and dbRef

I am migrating from the Firebase namespaced to modular API. There is one detail I do not fully understand.

I have initiated and exposed the database in a separate file using:

import { initializeApp } from "firebase/app";
import { getDatabase } from "firebase/database";

const firebaseConfig = {
  apiKey: process.env.FIREBASE_API_KEY,
  authDomain: process.env.FIREBASE_AUTH_DOMAIN,
  databaseURL: process.env.FIREBASE_DATABASE_URL,
  projectId: process.env.FIREBASE_PROJECT_ID,
  storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
};

const app = initializeApp(firebaseConfig);
const database = getDatabase(app);

export { database };

But, a the file where database is imported from the above, I can write

import { database } from './firebase/firebase';
import { getDatabase } from "firebase/database";

const dbRef = ref(getDatabase());
get(ref(dbRef, `${dbPathAccess}/${uid}`)).then((mySnapshots) => {

instead of

import { database } from './firebase/firebase';

get(ref(database, `${dbPathAccess}/${uid}`))

My question is:

Are there any negative side-effects of using getDatabase() or is it good practice to use getDatabase() opposed to just ref(database)?

I cannot seem to find any extensive documentation on how getDatabase() works.

2

Answers


  1. other then the mistake in the code as highlighted by the @RenaudTarnec in above comment ,here i am going to just focus on the subject.

    well there is not really any difference between using getDatabase() or using ref(database) the core aim of both methods is to access the firebase realtime database

    but as you asked from the point of stander or good practice to follow then you should understand that there is no negative effect of using getDatabase(). people consider it as better practice because it provide better readability and maintainability. Please try to go through the following explanation.

    if you and your team-mates are using the one type lets say getDatabase() or ref(database) then for the shake of consistency just stick with that one.

    other view is that if you use getDatabase() in your project it provide better readability and understanding to other members because by just looking at the word getDatabase every one gets to know from here we are accessing the database on the other hand ref(database) may not be that obvious of the new Joiners to understand just by word.

    also if you use getDatabase() and you wish to modify the initialization file according to your future needs then you have to just make changes on one place and just with that it will be reflected in all of your project ,on the other hand if you uses ref(database) then you gonna have to update every instance where database is imported and of-course it will consume more time.

    Login or Signup to reply.
  2. The getDatabase function returns a Database object., while the ref function returns a DatabaseReference object. These are different object, and you should get the object that you need.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search