提取文档后从Firestore获取文档ID [英] Get document id from Firestore after fetching document
问题描述
成功获取文档后,如何获取Firestore文档的文档ID.我正在从这样的Firestore集合中获取用户数据
How can I get the document id of a firestore document after successfully fetching the document. I am fetching user data from firestore collections like this
const ref = await db.collection('users').where('phone', '==', phone)
.where('password', '==', password).get();
执行此查询后,我有一个有效的文档.
I have a valid document after executing this query.
const data = ref.docs[0].data();
如何获取自动生成的文档ID?我尝试了 data.id
和 data.path
,它们返回的是未定义的.
How can I get the auto generated document id? I tried data.id
and data.path
, it is returning undefined.
如何访问自动生成的ID?
How can I access the auto generated id?
推荐答案
在我的代码中,它实际上是在尝试获取数据ID,而不是文档ID我通过添加 const docRefId = ref.docs [0] .id;
In my code, it was actually trying to get the data id, instead of document id
i got the document id by adding const docRefId = ref.docs[0].id;
这篇关于提取文档后从Firestore获取文档ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!