如何检索单个 Firestore 文档的 ID? [英] How to retrieve the Id of a single Firestore document?

查看:32
本文介绍了如何检索单个 Firestore 文档的 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

import { Component, OnInit } from '@angular/core';

import { AngularFirestore
       , AngularFirestoreCollection
       , AngularFirestoreDocument } from 'angularfire2/firestore';

import { Observable } from 'rxjs/Observable';

interface Country {
  id?: string;
  name?: string;
  code?: string;
  flag?: string;
  continent?: string;
}


@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
    title = 'Firestore - Documents';

    private countryRef: AngularFirestoreCollection<Country>;
    docId: any;

    constructor( private afs: AngularFirestore ) {

        this.countryRef = this.afs.collection('Country', ref => ref.where('code', '==', 'za'));

        this.docId = this.countryRef.snapshotChanges().map( changes => {
            return changes.map(a => {
                const data = a.payload.doc.data() as Country;
                data.id = a.payload.doc.id;
            return data.id;
            });
        });

    console.log(this.docId);

  }

  ngOnInit() {}

}

我期待一个丑陋的 Firestore id 但我得到了这个:

I am expecting an ugly firestore id but instead I am getting this:

Observable {_isScalar: false, source: Observable, operator: MapOperator}

推荐答案

You are getting data as Observable const data = a.payload.doc.data() as Country

You are getting data as Observable const data = a.payload.doc.data() as Country

需要订阅才能获取数据

this.docId.subscribe(docs => {
  docs.forEach(doc => {
    console.log(doc.id);
  })
})

这是推荐的方法

export class AppComponent implements OnInit {
title = 'Firestore - Documents';

private countryRef: AngularFirestoreCollection<Country>;
docId: Observable<Country[]>;

constructor( private afs: AngularFirestore ) {

    this.countryRef = this.afs.collection('Country', ref => ref.where('code', '==', 'za'));

    this.docId = this.countryRef.snapshotChanges().map( changes => {
        return changes.map(a => {
            const data = a.payload.doc.data() as Country;
            const id = a.payload.doc.id;
            return { id, ...data };
        });
    });

this.docId.subscribe(docs => {
  docs.forEach(doc => {
    console.log(doc.id);
  })
})

}

  ngOnInit() {}

}

使用 angularfire2 从 firestore 检索数据的最常见做法是 .valueChanges().snapshotChanges().valueChanges() 方法仅提供数据.它去除包括 keys 在内的所有元数据.另一方面,.snapshotChanges() 将返回包括元数据在内的所有数据.

Most common practice to retrieve data from firestore using angularfire2 are .valueChanges() and .snapshotChanges(). valueChanges() method provides only the data. It strips all meta data including keys. On other hand .snapshotChanges() will return all data including metadata.

在您的代码中,当您执行 const data = a.payload.doc.data() as Country; 时,它只返回没有键的数据.当您将其映射到 const data 时,id 将被忽略,因为您指定了像 id?: string; null 安全模式这样的构造函数.

In your code when you do const data = a.payload.doc.data() as Country; it only returns the data with out key. and when you map it to const data id will be ignored because you specified your constructor like id?: string; null safe mode.

然后你得到 id const id = a.payload.doc.id; 并且你需要以你想要的 interface 方式返回它.通过这样做 return { id, ...data }; 你也返回了所有带有 id 的数据.并且 ...data 将其所有字段一一附加在 id 之后.您可以了解有关此功能的更多信息 这里希望你理解.

Then you get the id const id = a.payload.doc.id; and somehow you need to return it the way you want your interface is. By doing this return { id, ...data }; you are returning all data with id too. and ...data will append all its field one by one after id. you can learn more about this feature here Hope you understand.

这篇关于如何检索单个 Firestore 文档的 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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