Angularfire5 - Get the key of Realtime database documents in list - #askfirebase

让人想犯罪 __ 提交于 2019-12-13 03:13:47

问题


I am loading a list and passing it to a component to then render the information. One of the items I need is the ID. How can I see that in the list. I've seen the documentation and it does not help. It only shows the key of the parent.

this.allWorkouts = this.afDb.list<IWorkout>('/workouts')
    .valueChanges()
    .take(1)
    .map((array) => array.reverse()) as Observable<IWorkout[]>;

<ion-card *ngFor="let workout of allWorkouts | async">
    <card-workout [workout]="workout"></card-workout>
</ion-card>

回答1:


In version 5.0 of AngularFire you need to use snapshotChanges() if you want to get access to the key.

valueChanges() is only for basic use cases were you only care about the JSON tree.




回答2:


To get the key you need suscribe the observable and then print of this way:

suscribe(snapshot => { snapshot.$key })

If you need print into a *ngFor you should use:

workout.$key


来源:https://stackoverflow.com/questions/46741561/angularfire5-get-the-key-of-realtime-database-documents-in-list-askfirebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!