Home > database >  firebase realtime database undefined function
firebase realtime database undefined function

Time:03-03

I'm trying to retrieve from a realtime db I started by init the app with the configuration like so

import {initializeApp} from 'firebase/app';
import {getDatabase} from 'firebase/database';
let config = {
  apiKey: 'xxxxxxxxxxxx',
  authDomain: 'xxxxxxxxxxxx',
...
};
const app = initializeApp(config);
const database = getDatabase(app);

then when I try using the database variable it crashes

  componentDidMount() {
    database
      .ref(`master/tickets`)
      .once('value', snapshot => {
        var obj = snapshot.val();
        console.log('obj :', obj);
      })
      .catch(error => {
        console.log(error);
      });
  }

I also have tried firebase.database.ref() it shows error cannot find variable firebase What I am doing wrong? It was working fine in previous builds

CodePudding user response:

The Firebase Web SDK underwent major breaking changes with the update to V9.0.0.

Please see the upgrade guide for details on how to modify older patterns to the new API.

import { ref, get } from "firebase/database";
import { database } from "...";

/* ... */

componentDidMount() {
  get(ref(database, 'master/tickets'))
    .then(snapshot => {
      const obj = snapshot.val();
      console.log('obj :', obj);
    })
    .catch(error => {
      console.log(error);
    });
}
  • Related