亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

在一個(gè)嵌套集合中檢索文檔
P粉682987577
P粉682987577 2024-03-22 13:50:45
0
1
1421

我正在開發(fā)一個(gè)React項(xiàng)目,在這個(gè)項(xiàng)目中,我想要列出已登錄用戶的文檔。結(jié)構(gòu)如下,我想要讀取的文檔位于集合中。

數(shù)據(jù)庫(kù)結(jié)構(gòu)如下:

users(集合) -> user(文檔) -> repos(集合) -> repo(文檔) -> files(集合) -> files(文檔)

我想要讀取的是repo(文檔)。(它還有一些其他字段)。

這是我嘗試的代碼:

const userRef = doc(db, "users", userId)
const repoRef = collection(userRef, "repos")

const querySnapshot = await getDocs(repoRef);

querySnapshot.forEach((doc) => {
  console.log(doc.id, " => ", doc.data());
}

錯(cuò)誤信息:

FirebaseError: 期望的類型是 'DocumentReference',但實(shí)際上是:一個(gè)自定義的CollectionReference對(duì)象

P粉682987577
P粉682987577

全部回復(fù)(1)
P粉418351692

如果您只想查詢一個(gè)文檔,則需要指定您的倉(cāng)庫(kù)文檔 ID:

import { doc, getDoc } from "firebase/firestore"

const docRef = doc(db, `users/${userId}/repos/${repoDocId}`);
const docSnapshot = await getDoc(docRef);

console.log("repo doc data:", docSnapshot.data())

如果您想查詢所有倉(cāng)庫(kù),則需要查詢集合:

import { collection, query, where, getDocs } from "firebase/firestore";

const querySnapshot = await getDocs(
  query(collection(db, `users/${userId}/repos`))
)

querySnapshot.forEach((doc) => {
  console.log(doc.id, " => ", doc.data())
});

您可以在此處找到更多相關(guān)信息。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板