如何使用 Javascript 从 Firebase read/retrieve 获取数据
How to read/retrieve data from Firebase using Javascript
我正在尝试使用控制台日志从 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>
您正在尝试将旧的 v8 语法与 Firebase SDK 9 结合使用,但这行不通。您要么必须使用新的模块化语法,要么导入旧的 SDK 或 v9 上的兼容版本。
在 v9 语法中,从数据库中获取 reference to and reading 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 specifically the section on upgrading with the compat libraries.
我正在尝试使用控制台日志从 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>
您正在尝试将旧的 v8 语法与 Firebase SDK 9 结合使用,但这行不通。您要么必须使用新的模块化语法,要么导入旧的 SDK 或 v9 上的兼容版本。
在 v9 语法中,从数据库中获取 reference to and reading 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 specifically the section on upgrading with the compat libraries.