Firestore 从集合中获取文档 ID [英] Firestore Getting documents id from collection
问题描述
我正在尝试使用 id 检索我的文档,但无法弄清楚.
目前我检索我的文件是这样的:
I'm trying to retrieve my documents with id but can't figure it out.
Currently I retrieve my documents like this :
const racesCollection: AngularFirestoreCollection<Races> = this.afs.collection('races');
return racesCollection.valueChanges();
我确实完美地获得了我的文档列表,但是它们没有文档 ID.
I do get my documents list perfectly, however there is no doc id with them.
如何为每个文档检索它?
How can I retrieve it for each document ?
推荐答案
我终于找到了解决方案.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
这篇关于Firestore 从集合中获取文档 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!