2

Image shown below for more clear idea

I'm using reactjs I want to delete a specific document which is shown in the red box. the document might contain some data. I want to delete the document completely with data inside it. I used the following code to delete the document

    db.collection(currentUser.uid).doc(documentName).delete().then(function() {
    console.log("Document successfully deleted!");
}).catch(function(error) {
    console.error("Error removing document: ", error);
});

This is giving me inconsistent results. If I click on the delete button, It will not delete the document. When I refresh the page and click on the button again then it will delete it from firebase. When I click on delete button without refresh, it gives red and green colour animation to document name and also to its data like it is deleting it and again updating it as you can see in the image.

It would be great if anyone could give me a solution

Gaurav Thakur
  • 813
  • 7
  • 8
  • Do you see any error in the web console? Also, do you have some specific Firestore security rules in place? – Renaud Tarnec Sep 20 '20 at 08:51
  • In the console, it shows document successfuly deleted. No, read and write rules specified – Gaurav Thakur Sep 20 '20 at 08:53
  • Can you share your security rules please? – Renaud Tarnec Sep 20 '20 at 08:54
  • here are rules https://pastebin.pl/view/9e92ce2b – Gaurav Thakur Sep 20 '20 at 09:02
  • Ok, thanks. I don't see any reason why the piece of code you show in your question would not work correctly. There is maybe an interaction with some other code, but we cannot say for sure, based on what is in your question. – Renaud Tarnec Sep 20 '20 at 09:13
  • @RenaudTarnec I got the problem. I noticed one thing, if the document is empty then it is able to delete it. So I need to first empty it. I am unable to delete the field value, which in case is "tasks". Please help me to delete the field value – Gaurav Thakur Sep 20 '20 at 09:32
  • I've never heard a problem like this one with the [`delete()`](https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#delete) method.You should be able to delete the doc without problem if the `DocumentReference` is correct and the access rights allow it. Are you sure that `currentUser.uid` and `documentName` have a value when you call the function? Can you add some `console.log()` to confirm it. – Renaud Tarnec Sep 20 '20 at 11:57
  • @RenaudTarnec here is a video with console.log() https://youtu.be/5SbyfIPT9_o – Gaurav Thakur Sep 20 '20 at 14:18
  • 1
    Thanks Gaurav. I can clearly see the problem but it is impossible for me to tell you what's happening without more debugging details. It seems that you (re)create the doc first and then delete it. You should debug your code more thoroughly. Like I said, there is probably some other code implied in the problem (the one that creates the doc). – Renaud Tarnec Sep 20 '20 at 14:50
  • Thank you @RenaudTarnec, I will try to see what is causing the problem and let you know once it is solved – Gaurav Thakur Sep 20 '20 at 14:53
  • @RenaudTarnec The problem has been solved. You said it right, the one who is creating the doc is creating the problem. I updated the function and now It is not updating the value while deleting it. Once again Thank You – Gaurav Thakur Sep 20 '20 at 16:16
  • There is no client SDK operation for deleting a document and all nested subcollections. Deleteing a documetn with delete() will not delete the subcollecitons, just the one document. You will need to find a way to delete the documents in the subcollections separately. – Doug Stevenson Sep 20 '20 at 16:43

1 Answers1

0

Firebase SDK was doing its job but there was some problem with my implementation.

Gaurav Thakur
  • 813
  • 7
  • 8