Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I would like to prevent writing when there is the same document ID in firestoreb

enter image description here

I would like to prevent writing when there is the same document ID in firestoreb.

I am new to flutter.
As the title says, when writing from flutter to firestore, if the same document ID exists, it is usually overwritten.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So, I would like to prevent the same document ID from being overwritten.

Is there a good way to do this?

>Solution :

The most straightforward way is to use a security rule as follows:

match /kyousyoku/{docId} {
  allow write: if !exists(/databases/$(database)/documents/kyousyoku/$(docId))
}

A small drawback of this approach is the fact that the error message sent back to the client doesn’t indicate that the write has failed because the document already exists.

If you want to be able to make the distinction between this error and other errors returned by the security rules (e.g. the user is not authenticated) you can use a Transaction.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading