gpt4 book ai didi

javascript - 如何连接到 Firebase Firestore?

转载 作者:行者123 更新时间:2023-12-04 09:49:19 24 4
gpt4 key购买 nike

我想从 Firestore 数据库中获取数据,但是,我得到的不是 1 个元素的数组,而是 [Al],我不确定它是什么。

这是我在 Index.html 中的代码。出于安全原因,我在这里更改了配置对象中的信息。

<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-firestore.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-analytics.js"></script>

<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "my api key",
authDomain: "demo",
databaseURL: "my url",
projectId: "my project",
storageBucket: "bucket",
messagingSenderId: "1051539927876",
appId: "my app id",
measurementId: "0000000000"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var db = firebase.firestore();
firebase.analytics();
</script>

<script src="app.js"></script>

这就是我在 app.js 文件中的内容

db.collection('cafes').get().then((snapshot) => {
console.log(snapshot.docs);
}).catch(err => {
console.log(err);
});

最佳答案

您需要执行以下操作来获取数据:

db.collection('cafes').get().then((snapshot) => {
snapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
});
}).catch(err => {
console.log(err);
});
doc.data()将包含文件的数据。

https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection

关于javascript - 如何连接到 Firebase Firestore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62037735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com