I am getting "Uncaught ReferenceError: firebase is not defined" error when i try to read my datas on Firebase
I am getting "Uncaught ReferenceError: firebase is not defined" error when i try to read my datas on Firebase
我在第 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>
firebase.database().ref()
是命名空间语法(在 V8 及之前使用),但您使用的是模块化 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);
});
我在第 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>
firebase.database().ref()
是命名空间语法(在 V8 及之前使用),但您使用的是模块化 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);
});