如何在没有快照的情况下从 Angular firestore 文档获取 UID

How to get UID from an Angular fire store Document without snapshot

假设我有一个名为 Countries 的集合,文档如下所示

{
  name: "Pakistan"
  //other field removed to focus on problem only
}

和一个集合名称 Cities,其中的文档具有上述国家/地区字段参考,看起来像

{
  name: "Karachi",
  //other field removed to focus on problem only
  city: "3FbRFiWB4DmfdcxDdei3" //reference of country document
}

现在我将国家和城市显示为用户 select 国家/地区的有序下拉菜单,然后下一个下拉菜单显示过滤后的城市

let countryRef = this.angularFireStore.collection('Countries', ref => ref);
let countries = countryRef.valueChanges();

和html绑定看起来像

let country of country | async"

这是关于用户的任何更改或 select 事件的实际问题我必须将国家 ID 传递给基于 selected 国家/地区的人口稠密过滤城市,但我无法获得 ID国家。如何做到这一点 任何帮助将不胜感激。

注意:我知道我可以通过获取快照更改来获取 ID 和其他元数据,但是响应很晚,我只希望参考绑定中的 ID 字段用于编辑或更新文档。

正如您所说,您不想使用 .snapshotChanges() 并使用 .valueChanges() 获取 ID,您需要在创建文档时存储它。在您的添加国家/地区功能中执行此操作

this.tempId = this.angularFireStore.createId();

const newCountry = {
      id:this.tempId,
      name:'Karachi'
      //other fields...
    }

this.angularFireStore.collection('Countries').doc(this.tempId).set(newCountry).then(res => {
  console.log('country added')
}).catch(err => {
  console.log('error adding country',err)
});