【问题标题】:How to read/retrieve data from Firebase using Javascript如何使用 Javascript 从 Firebase 读取/检索数据
【发布时间】:2022-01-20 23:08:46
【问题描述】:

我正在尝试使用控制台日志从 firebase 获取我的数据,但出现错误。 错误是:image 这是我的数据库:https://i.stack.imgur.com/A1zYm.png

<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 SDK 9 中使用旧的 v8 语法,但该语法不起作用。您要么必须使用新的模块化语法,要么导入较旧的 SDK 或 v9 上的兼容版本。

    在 v9 语法中,从数据库中获取 referencereading a value 是通过以下方式完成的:

    import { getDatabase, ref, get } from "firebase/database";
    
    ...
    
    const database = getDatabase();
    
    const firebaseRef = ref(database, "Users");
    get(firebaseRef, function(snapshot) {
      snapshot.forEach(function(element) {
        console.log(element);
      })
    });
    

    有关更多示例,请参阅我上面链接的文档中的 Web 版本 9(模块化) 选项卡,以及 upgrade guide 特别是 upgrading with the compat libraries 部分。

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多