Search code examples
javascriptfirebasegoogle-cloud-firestorefirebase-security

Firebase Firestore deleteDoc() not working


I am working on my first Web App using firebase and I have hit a problem I can't find the solution to. I am trying to delete a document by Id, The function works fine however the document is not deleted. I have set the Firestore rules to allow delete as well. However it's still not working. I'll leave the relevant code and rules here:

Code

function del(x){
      console.log("Delete File : "+filesListID[x-1]);
      console.log("File : "+fileList[x-1].fileno + " " +decrypt(fileList[x-1].fileno));
      deleteDoc(doc(firestore,"property",filesListID[x])).then( function(){
          var table = document.getElementById("userlist");
          table.innerHTML = "";
          userList();
          alert("File Deleted Successfully");
      }).catch((error) => {
        console.log("Error Deleting Property List: "+error);
      });
      
}

Rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write, delete: if request.auth != null;
    }
  }
}

I can't figure out what's going wrong here, any suggestions will be helpful, Thanks!


Solution

  • I believe you want to delete filesListID [x-1] but inside deleteDoc() you have mentioned filesListID [x]. And at the top of that, before deleting, you could also check if filesListID [x-1] exists or not.

    function del(x) {
      console.log("Delete File : " + filesListID[x - 1]);
      console.log("File : " + fileList[x - 1].fileno + " " + decrypt(fileList[x - 1].fileno));
      deleteDoc(doc(firestore, "property", filesListID[x - 1])).then(function() {
        var table = document.getElementById("userlist");
        table.innerHTML = "";
        userList();
        alert("File Deleted Successfully");
      }).catch((error) => {
        console.log("Error Deleting Property List: " + error);
      });
    }