简体   繁体   English

如何使用 angular Firestore 在 Firestore 中获取集合 ID

[英]How to get ID of collection in Firestore with angular Firestore

I'm not able to get the ID of the document when I query a Firestore Database this way:当我以这种方式查询 Firestore 数据库时,我无法获取文档的 ID:

Could you give me some help?你能给我一些帮助吗?

import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection  } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
export interface Item { name: string; }

@Component({
  selector: 'app-annonces-contactees',
  templateUrl: './annonces-contactees.page.html',
  styleUrls: ['./annonces-contactees.page.scss'],
})
export class AnnoncesContacteesPage implements OnInit {
  private annoncesCollection: AngularFirestoreCollection<Item>;
  annonces: Observable<Item[]>;
    constructor(private afs: AngularFirestore) {
      this.annoncesCollection = afs.collection('annonces', ref => ref.where('prix', '>=', 1000000))
      this.annonces = this.annoncesCollection.valueChanges();
   }

  ngOnInit() {
}

}

I am going to give you an example of how I dot it: Let us suppose I have collection of hospitals and each hospital has its name,phone and location.我将举一个例子来说明我是如何打点它的:让我们假设我有医院的集合,每个医院都有它的名称、电话和位置。

  constructor(private firestore:AngularFirestore){}
  hospitalsArray=[];
  ngOnInit(){
      this.firestore.collection("hospitals").snapshotChanges().subscribe((data) => {
        this.hospitalsArray = data.map(e => {
          return { id: e.payload.doc.id, location: e.payload.doc.data()["location"], number: e.payload.doc.data()["phone"], name: e.payload.doc.data()["name"]}
        })
  }

"hospitals" is the name of the collection and this id is the id of the document. “hospitals”是集合的名称,这个 id 是文档的 id。

So if you want to display in the html file所以如果要在html文件中显示

<ion-item *ngFor="let hospital of hospitalsArray">
<ion-label>{{hospital.name}}</ion-label>
</ion-item>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM