Home > Mobile >  I am getting "Uncaught ReferenceError: firebase is not defined" error when i try to read m
I am getting "Uncaught ReferenceError: firebase is not defined" error when i try to read m

Time:12-18

I am getting the error in 43th code line. Where am i doing wrong? Also my database's image here: This is my database

<script type="module">
  import {
    initializeApp
  } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-app.js";
  import {
    getDatabase,
    set,
    ref,
    update
  } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-database.js";
  import {
    getAuth,
    createUserWithEmailAndPassword,
    signInWithEmailAndPassword,
    onAuthStateChanged,
    signOut
  } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-auth.js";
  const firebaseConfig = {
    apiKey: ,
    authDomain: ,
    databaseURL: ,
    projectId: ,
    storageBucket: ,
    messagingSenderId: ,
    appId: ,
    measurementId:
  };
  const app = initializeApp(firebaseConfig);
  const db = getDatabase(app);
  const auth = getAuth();
  const firebaseRef = firebase.database().ref("Users");
  firebaseRef.once("value", function(snapshot) {
    snapshot.forEach(function(element) {
      console.log(element);
    })
  });
</script>

CodePudding user response:

The firebase.database().ref() is the namespaced syntax (used on V8 and before) but you are using Modular SDK (V9 ). Try refactoring the code as shown below using ref() and get() functions:

// ...imports
// import { get, ref } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-database.js"

const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
const auth = getAuth(app);

const firebaseRef = ref(db, 'Users');

get(firebaseRef).then((snapshot) => {
  if (snapshot.exists()) {
    console.log(snapshot.val());
  } else {
    console.log("No data available");
  }
}).catch((error) => {
  console.error(error);
});
  • Related