Home > Enterprise >  Having error attempting to use RTDB in Expo managed
Having error attempting to use RTDB in Expo managed

Time:01-30

I'm attempting to use firebase realtime database on my app. I first authenticate and then I need to use the RTDB. Here is my attempt.

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

import getEnvVars from '../environment';
const ENV = getEnvVars();

const firebaseConfig = {
  apiKey: ENV.firebase.API_KEY,
  authDomain: ENV.firebase.AUTH_DOMAIN,
  databaseURL: ENV.firebase.DATABASE_URL,
  projectId: ENV.firebase.PROJECT_ID,
  storageBucket: ENV.firebase.STORAGE_BUCKET,
  messagingSenderId: ENV.firebase.MESSAGING_SENDER_ID,
  appId: ENV.firebase.APP_ID
};



initializeApp(firebaseConfig);

export const db = getDatabase();

The error I get is:

TypeError: (0, _database.getDatabase) is not a function. (In '(0, _database.getDatabase)()', '(0, _database.getDatabase)' is undefined)

CodePudding user response:

You're mixing up the old, namespaced syntax of SDK versions 8 and lower, with the new, modular syntax of SDK versions 9 and above.

To get the database in v9, you call getDatabase() as a top-level function:

export const db = getDatabase();

I recommend keeping the Firebase documentation ready when going through this, as it has code samples for both the old and new syntax so you can easily compare. For this specific call, see the documentation on getting a database reference.

  •  Tags:  
  • Related