Firestore从集合中获取文档ID [英] Firestore Getting documents id from collection

查看:130
本文介绍了Firestore从集合中获取文档ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索具有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(),然后按照此处所述正确映射它

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆