且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firestore 从集合中获取文档 ID

更新时间:2023-02-14 09:32:23

我终于找到了解决方案.Victor 非常了解文档数据.

I've finally found the solution. Victor was close with the doc data.

const racesCollection: AngularFirestoreCollection<Race>;
return racesCollection.snapshotChanges().map(actions => {       
  return actions.map(a => {
    const data = a.payload.doc.data() as Race;
    data.id = a.payload.doc.id;
    return data;
  });
});

ValueChanges() 不包含元数据,因此当我们需要文档 ID 时我们必须使用 SnapshotChanges(),然后按照此处所述正确映射它 https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md

ValueChanges() doesn't include metadata, therefor we must use SnapshotChanges() when we require the document id and then map it properly as stated here https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md