从云端Firestore获取自动生成的ID

时间:2019-02-23 10:40:32

标签: angular

我从我的角度组件的云存储中获取了数据,如下面的代码所示

服务文件

import { Injectable } from '@angular/core';
import 
{AngularFirestoreDocument,AngularFirestore,AngularFirestoreCollection} 
from 'angularfire2/firestore';

import { Observable } from 'rxjs';


@Injectable({
providedIn: 'root'
})
export class ItemsService {
itemscollection:AngularFirestoreCollection<any>
items:Observable<any[]>

constructor(public afs:AngularFirestore,db:AngularFirestore) { 


}

getitems(){

  return   

this.items=this.afs.collection('categories',ref=>ref.orderBy('name'))
.valueChanges()  

}

}

.component.ts文件

export class AdminNewComponent implements OnInit {
@ViewChild('f') form:NgForm
categories
default="Bread"
constructor(public iservice:ItemsService,private 
prservice:ProductsService,private router:Router,private 
route:ActivatedRoute) { }

ngOnInit() {
this.iservice.getitems().subscribe(items=>{
  this.categories=items
  // console.log(this.categories)

})
}

然后将其渲染到html组件中 但是我遇到的问题是如何检索数据库中所有自动生成的ID,如下面的屏幕快照所示

https://ibb.co/fp3MBF0

1 个答案:

答案 0 :(得分:0)

使用snapshotChanges()代替valueChanges()。看起来像这样...

export const mapFireBaseListResponse =(list:AngularFireList)=>

list.snapshotChanges().pipe(
    map(items => 
        items.map(item => ({
                key: item.payload.key, 
                ...item.payload.val()
            })
        )
    )
);