简体   繁体   English

如何删除firestore中自动生成的id数据

[英]How to delete auto-generated id data in firestore

async created () {
    const sn = await db.collection('forms').get()
    sn.forEach(v => {
      const { title, content } = v.data()
      this.forms.push({
        title, content, id: v.id
      })
      console.log(v.id)
    })
  },
  del () {
    db.collection('forms').doc().delete()
  }
}

I succeeded in reading the data and getting an ID.我成功地读取了数据并获得了一个 ID。 But I don't know what to put in the doc() to delete.但我不知道在 doc() 中要删除什么。

If you knew the ID of the document then it would have been as easy as this:如果您知道文档的 ID,那么它会像这样简单:

firebase.firestore().collection("forms").doc("docId").delete()

But in your case, you would have to add an additional step of getting the document ID.但在您的情况下,您必须添加一个额外的步骤来获取文档 ID。 I'm not sure about your db structure but for example, let's say you want to delete a form submitted by a user named "john" (you must store the name in document).我不确定您的数据库结构,但例如,假设您要删除名为“john”的用户提交的表单(您必须将名称存储在文档中)。 Then you can fetch the document (and it's ID) this way:然后您可以通过以下方式获取文档(及其 ID):

const formDoc = await firebase.firestore().collection("forms").where("name", "==", "john").get()
if (formDoc.exists) {
  await firebase.firestore().collection("forms").doc(formDoc.id).delete()
}

Again the name may not be unique so it's better to rely on user UIDs for this.同样,名称可能不是唯一的,因此最好依靠用户 UID。 If you are using Firebase Authentication then please add a field in document called uid having the value of user's UID in it.如果您使用的是 Firebase 身份验证,请在名为 uid 的文档中添加一个字段,其中包含用户 UID 的值。 Then you can use UID instead of name in the query.然后您可以在查询中使用 UID 而不是名称。

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

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