Reputation: 1
I appreciate your response in advance.
It happens that I am trying to delete a document from firebase (Cloud Firestore). I need the ID for that. But the id is not in the array I get when I run: return this.db.collection ('enterprises').ValueChanges();
The array that delivers that line of code only includes the properties that I wrote only (name, email)
Only by running this.db.collection('enterprises').snapshotChanges();
I get another fix, from where I get the id's in the document (fix.payload.doc.id). and the information array through: fix.payload.doc.data();
The problem is that the Id array I get it apart, and not inside the previous array. To fix it I made an array mix and so I left the above array with id as a property. But the question is: Is that the way to do it? Or is there another way to delete the document without having to do that mix of arrays? (I did it with a foreach)
Users.service.ts (afs is AngularFirestore)
mixInfo() {
return this.afs.collection('users').snapshotChanges();
}
deleteUser(id: string) {
this.afs.collection('users').doc(`${id}`).delete();
}
users.component.ts
mixInfo() {
this.userService.mixInfo().subscribe(r => {
r.forEach(user => {
const id = user.payload.doc.id;
const data = <UserInterface>user.payload.doc.data();
const { name, email, password, role } = data;
this.userService.editUser(id, { id, name, email, password, role })
this.getUsers();
});
});
}
onDelete(id) {
this.userService.deleteUser(id);
}
Template
<tbody>
<tr *ngFor="let user of users; let i = index">
<td contenteditable="true" (blur)="onEdit(user, 'name', $event)">{{user.name}}</td>
<td contenteditable="true" (blur)="onEdit(user, 'email', $event)">{{user.email}}</td>
<td contenteditable="true" (blur)="onEdit(user, 'role', $event)">{{user.role}}</td>
<td>
<a *ngIf="user.role !== 'Administrador'" (click)="onDelete(user.id)" class="delete" title="Delete" data-toggle="tooltip"><i class="material-icons"></i></a>
</td>
</tr>
</tbody>
Thanks!
Upvotes: 0
Views: 429
Reputation: 317467
In order to write or delete a document in Firestore, you need to know its full path, including the names of collections and documents in that path. There are no alternatives to this. So, either you 1) know the ID ahead of time, or you 2) make a query for it. It sounds like you don't know the ID, but are able to make a query to find the document to delete. This will have to be your option to delete it.
Upvotes: 1