Firestore - 如何从 FirebaseFirestore.QuerySnapshot 获取文档 ID?

Con*_*eer 5 firebase typescript google-cloud-firestore

我正在查询特定文档的集合,并尝试从 FirebaseFirestore.QuerySnapshot 类型的查询结果中获取文档 ID。

我的实际查询如下所示(我正在使用 async/await):

result = await db.collection("chatrooms").where("userA", "==", req.body.userA)
            .where("userB", "==", req.body.userB).limit(1).get();
Run Code Online (Sandbox Code Playgroud)

我尝试过:

await result.docs.map(doc => {
            return doc.id
        })
Run Code Online (Sandbox Code Playgroud)

但这给了我一个undefined支持。

我究竟做错了什么?

imj*_*red 7

QuerySnapshot 包含零个或多个表示查询结果的 DocumentSnapshot 对象。文档可以通过 docs 属性作为数组进行访问,也可以使用 forEach 方法进行枚举。文档的数量可以通过empty 和size 属性来确定。

所以它将返回QueryDocumentSnapshots 的数组 (QuerySnapshot)。您需要使用forEach()循环快照,然后您可以从每个快照中获取文档 IDQueryDocumentSnapshot

https://firebase.google.com/docs/reference/js/firebase.firestore.QueryDocumentSnapshot

如果您使用的是 typescript,您应该能够在 IDE 中看到定义、方法和属性。