我是 Firestore 的新手,我正在尝试在 app.js 中使用 db,但是当我运行 html 文件时,我在控制台上收到此错误(Uncaught ReferenceError: db is not defined)。我可以知道如何解决这个问题吗?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div>
<h1>Cloud cafe</h1>
<div class="content">
<form id="add-cafe-form"></form>
<ul id="cafe-list"></ul>
</div>
</div>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-analytics.js";
import { getFirestore} from "https://www.gstatic.com/firebasejs/9.0.1/firebase-firestore.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
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyA_iQKn0dazcTipquleNaF0df32KdWKia4",
authDomain: "ninja-flutter-tut.firebaseapp.com",
projectId: "ninja-flutter-tut",
storageBucket: "ninja-flutter-tut.appspot.com",
messagingSenderId: "876805702468",
appId: "1:876805702468:web:cd2cc55cc394f94726cd11",
measurementId: "G-9HDMHXT2PY"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getFirestore(app);
</script>
<script src="app.js"></script>
</body>
</html>
下面是app.js的代码
// getting data
db.collection('cafes').get().then(snapshot => {
console.log("test");
});
最佳答案
最可能的原因是您的 app.js 文件在您定义 db 变量之前被执行。你可能想在你的 app.js 脚本中添加一个 defer 语句,因为你在它上面的脚本中定义了 db 变量,就像这样:
<script src="app.js" defer></script>
最好的办法是在你的 app.js 文件中只包含第一个脚本内容,这样你就不会像这样有两个单独的脚本文件: 应用程序.js:
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-analytics.js";
import { getFirestore} from "https://www.gstatic.com/firebasejs/9.0.1/firebase-firestore.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
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyA_iQKn0dazcTipquleNaF0df32KdWKia4",
authDomain: "ninja-flutter-tut.firebaseapp.com",
projectId: "ninja-flutter-tut",
storageBucket: "ninja-flutter-tut.appspot.com",
messagingSenderId: "876805702468",
appId: "1:876805702468:web:cd2cc55cc394f94726cd11",
measurementId: "G-9HDMHXT2PY"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getFirestore();
// getting data
db.collection('cafes').get().then(snapshot => {
console.log("test");
});
关于javascript - Uncaught ReferenceError : db is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69010610/