Firebase 日志"undefined"不应在主机上显示



我正在使用firebase 10.9.2(最新版本)

我使用onValue函数来遍历实时数据库中的数据。由于某些原因,它总是记录(并被设置为)undefined

由于firebase的发展速度太快,我无法找到解决当前问题的方法。请告诉我如何处理代码,使其停止返回未定义。

// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.8/firebase-app.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyBU21edukO9sxUR13wYy6yYW9ZYK5ax4tM",
authDomain: "team-seas-demo.firebaseapp.com",
databaseURL: "https://team-seas-demo-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "team-seas-demo",
storageBucket: "team-seas-demo.appspot.com",
messagingSenderId: "864989587728",
appId: "1:864989587728:web:7eda88906cd9a7fe659d95"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
import {getDatabase, ref, set, child, update, remove, onValue}
from "https://www.gstatic.com/firebasejs/9.6.8/firebase-database.js"; 
var db = getDatabase(app);
onValue(ref(db, "Groups/"), function (snapshot){
const data = snapshot.val(); 
console.log(data); //This part works, but returns the whole object
console.log(data.GroupName) //This part returns undefined...
});

p。S:是的,我的读规则设置为true

这是我的数据库的样子:数据库图片

我认为最好使用onSnapshot()函数这里有一个例子https://firebase.google.com/docs/firestore/query-data/listen

最新更新