【发布时间】:2021-10-30 18:28:39
【问题描述】:
我是 Firestore 的新手,我正在尝试在 app.js 中使用 db,但是当我运行 html 文件时,我在控制台上收到此错误(未捕获的 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");
});
【问题讨论】:
标签: javascript html google-cloud-firestore