1

我想问一件事是否有可能获得满足条件的文档ID,就像我在特定集合上应用了条件并且查询是有效的并返回了文档,我想获得查询之后的文档ID

const reports = async() => {
const officeCollection = db.collection('XYZ');
  const officeQuerySnapshot = await officeCollection.where('office','==' ,office).get();
  const officeData = []
  var firstContact= ''
  officeQuerySnapshot.forEach(doc => {
    // var firstContact = doc.data().attachment['First Contact']
    // console.log(firstContact)
    officeData.push(doc.data())

  })
}
4

2 回答 2

1

要打印文档 ID,您需要使用doc.id

const reports = async() => {
const officeCollection = db.collection('XYZ');
  const officeQuerySnapshot = await officeCollection.where('office','==' ,office).get();
  const officeData = []
  var firstContact= ''
  officeQuerySnapshot.forEach(doc => {
    console.log(doc.id)
    officeData.push(doc.data())
  })
}
于 2020-02-25T14:44:58.430 回答
0

如果您将使用doc.ref而不是doc.data()您将参考将获得DocumentReference对象。比您可以获得像_path获取文档ID一样的属性。

我使用了类似的东西console.log(doc.ref._path),我得到了类似的东西:

在此处输入图像描述

文档 ID 在段列表中。您可以在此处找到 API 参考。

于 2020-02-25T09:49:45.067 回答