Home > Blockchain >  Firebase v9 Database. ref is not a function?
Firebase v9 Database. ref is not a function?

Time:02-18

I've started migrating over to v9 Modular firebase. Please see my config:

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

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
..
};

const app = initializeApp(firebaseConfig);
// Get a reference to the database service
export const database = getDatabase(app);

Then in another file I made basic CRUD functions.

import { database } from "./firebase"

export const insertTestData = () => {
    return database.ref("test").set({name:"hello world"})
}

I got the following error:

Uncaught TypeError: _firebase__WEBPACK_IMPORTED_MODULE_0__.database.ref is not a function

Did I miss anything?

I'm not sure why it's also not picking up imports

enter image description here

CodePudding user response:

You are importing ref() from Modular SDK but still using name-spaced syntax. Try:

import { getDatabase, ref, set } from "firebase/database";

export const insertTestData = () => {
  return set(ref(database, "test"), { name: "hello" })
}

Make sure you are referring to the "Modular" tab of the code snippets in the documentation

  • Related