簡體   English   中英

當我嘗試讀取 Firebase 上的數據時,出現“未捕獲的 ReferenceError:firebase 未定義”錯誤

[英]I am getting "Uncaught ReferenceError: firebase is not defined" error when i try to read my datas on Firebase

我在第 43 行代碼中遇到錯誤。 我在哪里做錯了? 還有我的數據庫圖片:這是我的數據庫

<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>

firebase.database().ref()是命名空間語法(用於 V8 及之前的版本),但您使用的是模塊化 SDK (V9+)。 嘗試使用ref()get()函數重構代碼,如下所示:

// ...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);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM