Angular Firebase Firestore获取文档ID [英] Angular Firebase Firestore get document ID
问题描述
我的Firebase Firestore数据库是这样的:
My Firebase Firestore database is like this:
Collection:orders
DocumentID: (autogenerated)
Document: orderName: string
这样声明的变量:
orderCollection: AngularFirestoreCollection<Order>
orders: Observable<Order[]>
我正在得到所有这样的订单":
I am getting all 'orders' like this:
orderCollection = this.afs.collection('orders');
orders = this.orderCollection.valueChanges();
然后我要打印这样的订单:
I am then printing the orders like this:
<tr *ngFor="let o of orders | async">
<td>{{o.orderName}}</td>
<td><a (click)="editOrder(o.$key)">Edit</a></td>
</tr>
直到编辑部分,一切都正常了.
I everything is working perfect up to the edit part.
对于如何发送回订单的文档ID?我想发回订单文件ID,以便随后对其进行编辑.使用实时数据库,您可以使用$ key来执行某些操作,但是不能使用firestore来执行某些操作.有建议吗?
For edit: how to I send back the document ID of the order? I want to send the order document ID back so I can then edit it. With the realtime database you could do something with $key, but not with the firestore. Suggestions?
推荐答案
在AngularFirestore中,它只是document.id
.但是,要使其正常工作,必须使用snapshotChanges
而不是valueChanges
.所以:
In AngularFirestore it is simply document.id
. But for this to work you must use snapshotChanges
instead of valueChanges
. So:
orders = this.orderCollection.snapshotChanges();
然后:
<tr *ngFor="let o of orders | async">
<td>{{o.data().orderName}}</td>
<td><a (click)="editOrder(o.id)">Edit</a></td>
</tr>
灵感来自: Firestore从集合中获取文档ID 和获取包含文档ID的集合.
这篇关于Angular Firebase Firestore获取文档ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!