Home > Net >  How to call database().ref() method in Firebase Config firebase V9
How to call database().ref() method in Firebase Config firebase V9

Time:12-31

I am working on a react project that I am I want to use both Auth and real-time database from firebase. I have managed to set auth but database import is giving me a hard time. I want to use the (database().child().push() ) method to push data to the database but I can't call the database().ref() method in my firebase Config. Anyone?

import { initializeApp } from "firebase/app";
import { getAuth } from 'firebase/auth'


// Your web app's Firebase configuration

const firebaseConfig = {
  apiKey: "AIzaSyD8aGnZYjhCTopgdNhuykWvmkJSHKj_h7E",
  authDomain: "bonnie-d17be.firebaseapp.com",
  databaseURL: "https://bonnie-d17be-default-rtdb.firebaseio.com",
  projectId: "bonnie-d17be",
  storageBucket: "bonnie-d17be.appspot.com",
  messagingSenderId: "397471611615",
  appId: "1:397471611615:web:3f6a743f4770ff8718c76a"

};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const authentication = getAuth(app);
export default app;

CodePudding user response:

Firebase's documentation has examples of both name-spaced (V8) and new functional (V9) syntax. To push a new node, try the following code:

// Initialize database similar to auth
import { getDatabase } from "firebase/database";

export const database = getDatabase();
import { push, child, ref } from "firebase/database";

const dbRef = ref(database)

push(child(dbRef, 'path/')).then(() => {
  console.log("Data added")
})
  • Related