3
votes

I'm testing the new firestore with ionic using angularfire2 and I'm able to list and create collections and documents following the documentation.

These are the ts and html file that I use in order to list a collection of documents.

According to the documentation in github I can use the delete() and update() methods in AngularFirestoreDocument but since I have a collection of documents I need the document path to do something like this this.chemicalsCollection.doc('path/1).delete() or this.chemicalsCollection.doc('path/1).update().

In the old days (last week) I could do something like this:

constructor(database: AngularFireDatabase) {
  this.itemsRef$ = this.database.list('items');
}
deleteItem(item) {
  this.itemsRef$.remove(item.$key)
}

and loop itemsRef$ in the html *ngFor="let item of chemicalsRef$ | async" (click)="deleteItem(item)". The index $key came from the firebase database but if I do the same with firestore there's no $key or path in the object, just the data I created.

What am I missing? how do I get the document's path?

1

1 Answers

4
votes

There are multiple ways of streaming data from a collection. I am guessing you are using .ValueChanges() which returns an Observable containing the JSON object data. What you need to be using is .snapshotChanges()It returns an array with a lot more information. Then you can use a map operator to get the data and the object Id from the array.

An example of this is show in the documentation under snapshotChanges(): AngularFire GitHub Doc