无法从Firestore中删除文档

时间:2020-08-03 06:16:32

标签: google-cloud-firestore

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

这是我的密码

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 b6f58a4e-6aa4-4e8a-9925-c3f7948739dc中的猫ID

确定:在此处从云中删除类别

用户ID = JFRImEwHsNMidmUqxhS80lXioDs1

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

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

类别已成功删除!

THEN:用户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);
    }
  }
}

在运行代码后,FIRSTORE看起来像什么:

左窗格:

类别

中潘:

类别

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

b6f58a4e-6aa4-4e8a-9925-c3f7948739dc

右窗格:

b6f58a4e-6aa4-4e8a-9925-c3f7948739dc

说明

ids

名称:“人物”

事物

1 个答案:

答案 0 :(得分:1)

发现该错误,我将字符串文字传递为userID而不是var,哑巴错误。直到firestore终于给我一条错误消息时才找到它,不知道为什么以前没有收到错误消息。