Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase v9 Database. ref is not a function?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading