Javascript 查询结果后是否获取文档id?

Javascript 查询结果后是否获取文档id?,javascript,node.js,google-cloud-firestore,Javascript,Node.js,Google Cloud Firestore,我想问一件事,是否有可能获得满足条件的文档id 就像我在特定集合上应用了一个条件,查询是有效的,并且返回了文档,我想得到查询后的文档id const reports = async() => { const officeCollection = db.collection('XYZ'); const officeQuerySnapshot = await officeCollection.where('office','==' ,office).get(); const offic

我想问一件事,是否有可能获得满足条件的文档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())

  })
}

如果您将使用
doc.ref
而不是
doc.data()
您将获得
DocumentReference
对象的引用。然后您可以获取属性,如
\u path
,以获取文档id

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

文档ID位于段列表中。
API参考您可以找到

要打印文档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())
  })
}