简体   繁体   English

如何删除 Firebase(Cloud Firestore)中的文档?

[英]How to delete a document in Firebase (Cloud Firestore)?

I appreciate your response in advance.感谢您提前回复。

It happens that I am trying to delete a document from firebase (Cloud Firestore).碰巧我正试图从 firebase(Cloud Firestore)中删除一个文档。 I need the ID for that.我需要那个ID。 But the id is not in the array I get when I run: return this.db.collection ('enterprises').ValueChanges();但是 id 不在我运行时得到的数组中: 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();仅通过运行this.db.collection('enterprises').snapshotChanges(); I get another fix, from where I get the id's in the document (fix.payload.doc.id).我得到了另一个修复,从那里获得文档中的 id (fix.payload.doc.id)。 and the information array through: fix.payload.doc.data();和信息数组通过: fix.payload.doc.data();

The problem is that the Id array I get it apart, and not inside the previous array.问题是 Id 数组我把它分开了,而不是在前一个数组中。 To fix it I made an array mix and so I left the above array with id as a property.为了解决这个问题,我做了一个数组混合,所以我把上面的数组留下了 id 作为属性。 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?还是有另一种方法可以删除文档而不必混合 arrays? (I did it with a foreach) (我用foreach做到了)

Users.service.ts (afs is AngularFirestore) Users.service.ts(afs 是 AngularFirestore)

mixInfo() {
   return this.afs.collection('users').snapshotChanges();
}

deleteUser(id: string) {
   this.afs.collection('users').doc(`${id}`).delete();
}

users.component.ts 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">&#xE872;</i></a>
     </td>
  </tr>    
</tbody>

Thanks!谢谢!

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.为了在 Firestore 中写入或删除文档,您需要知道其完整路径,包括 collections 的名称和该路径中的文档。 There are no alternatives to this.没有其他选择。 So, either you 1) know the ID ahead of time, or you 2) make a query for it.因此,要么您 1) 提前知道 ID,要么您 2) 对其进行查询。 It sounds like you don't know the ID, but are able to make a query to find the document to delete.听起来您不知道 ID,但能够进行查询以找到要删除的文档。 This will have to be your option to delete it.这必须是您删除它的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM