【问题标题】:Unable to delete document from firestore无法从 Firestore 中删除文档
【发布时间】:2020-11-23 05:36:36
【问题描述】:

我的 javascript 文件中有以下代码,它似乎运行良好,但是当我查看 firestore 控制台时,该文件仍然存在。如果我运行从 firestore 下载的初始化代码,它会将文档加载到我的应用程序中。数据库规则允许所有访问,因此在此处删除文档不会出现问题。事实上,我在其他地方也有类似的代码,它成功地从 Firestore 中删除了文档。抓挠我的头,这段代码应该从firestore中删除文档,但它不会。我整天用谷歌搜索这个问题,找不到任何解释或类似问题。我尝试删除并重新创建我的整个数据库,删除并创建了新用户。但是“类别”集合中的类别文档不会从代码中删除(从控制台是)

这是我的代码

deleteCategory({ commit }) {
    // save id before we delete from vuex so we can delete from firestore after
    let id_to_delete = state.categories[state.currentCategory].id
    console.log('idToDelete BEFORE = ', id_to_delete)
    console.log('cat id in vuex', state.categories[state.currentCategory].id)
    
    commit('deleteCategory')

    // now go out to database and delete this category
    console.log('HOOK: delete category from cloud here')
    console.log('user id=', state.userID)
    console.log('idToDelete AFTER = ', id_to_delete)
    let catRef = fs.collection('users')
      .doc('state.userID')
      .collection('categories')
      .doc(id_to_delete)
    
    console.log('catRef=', catRef)
    
    catRef.delete().then(function() {
        console.log('Category successfully deleted!')
        console.log('THEN: user id=', state.userID)
        console.log('THEN: idToDelete = ', id_to_delete)
      })
      .catch(function (error) {
        console.error('Error removing category: ', error)
      })
  },

这是我的控制台输出:

idToDelete BEFORE = b6f58a4e-6aa4-4e8a-9925-c3f7948739dc

vuex 中的猫 id b6f58a4e-6aa4-4e8a-9925-c3f7948739dc

HOOK:在此处从云中删除类别

用户 ID=JFRImEwHsNMidmUqxhS80lXioDs1

idToDelete AFTER = b6f58a4e-6aa4-4e8a-9925-c3f7948739dc

catRef= t {$c: t, firestore: t, hE: undefined, WT: t}

类别已成功删除!

那么:用户 ID = JFRImEwHsNMidmUqxhS80lXioDs1

那么:idToDelete = b6f58a4e-6aa4-4e8a-9925-c3f7948739dc

这是我的数据库规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone on the internet to view, edit, and delete
    // all data in your Firestore database. It is useful for getting
    // started, but it is configured to expire after 30 days because it
    // leaves your app open to attackers. At that time, all client
    // requests to your Firestore database will be denied.
    //
    // Make sure to write security rules for your app before that time, or else
    // your app will lose access to your Firestore database
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2021, 6, 24);
    }
  }
}

这是运行代码后 FIRESTORE 的样子:

左窗格:

分类

中间窗格:

分类

4f0e7a78-7787-41df-bea5-66a01f41a7c8

b6f58a4e-6aa4-4e8a-9925-c3f7948739dc

右窗格:

b6f58a4e-6aa4-4e8a-9925-c3f7948739dc

说明

身份证

姓名:“人”

事情

【问题讨论】:

    标签: google-cloud-firestore


    【解决方案1】:

    发现错误,我将字符串文字作为 userID 而不是 var 传递,愚蠢的错误。直到firestore最终给我一个错误消息才找到它,不知道为什么之前没有收到错误消息。

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 2021-07-01
      • 2020-10-31
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多