我正在嘗試使用控制臺日志從 firebase 獲取我的資料,但出現錯誤。錯誤是:影像 這是我的資料庫: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>
uj5u.com熱心網友回復:
您正嘗試在 Firebase SDK 9 中使用舊的 v8 語法,但該語法不起作用。您要么必須使用新的模塊化語法,要么匯入舊的 SDK 或 v9 上的兼容性版本。
在 v9 語法中,獲取對資料庫的參考并從資料庫中讀取值是通過以下方式完成的:
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 version 9 (modular)我上面鏈接的檔案中的選項卡,以及升級指南,特別是有關使用兼容庫升級的部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385218.html
