【问题标题】:I am getting "Uncaught ReferenceError: firebase is not defined" error when i try to read my datas on Firebase当我尝试在 Firebase 上读取数据时出现“未捕获的 ReferenceError:未定义 Firebase”错误
【发布时间】:2022-01-20 22:16:24
【问题描述】:

我在第 43 行代码中遇到错误。我在哪里做错了?还有我的数据库图片: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>

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

    firebase.database().ref() 是命名空间语法(用于 V8 及之前的版本),但您使用的是 Modular 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);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-17
      • 2016-10-20
      • 2016-04-30
      • 1970-01-01
      • 2020-10-08
      • 2021-06-16
      • 1970-01-01
      • 2017-01-02
      相关资源
      最近更新 更多