1

I am currently trying to verify before a user creates data in the database if the data exists. Even unter an other Document id.

Like this: User creates data, firestore rules gets all documents in collection and checks if the property name is the same as from the user provided. If yes, return unauthorized access.

At this point I have:

function checkIfCatExists(){
      return get(/databases/$(database)/documents/category/$(documents)).data.name != null;
    }

But this does not work. Do you guys have an idea? I could create a function for that but I want to do as much as possible with rules.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lukas Germerott
  • 361
  • 3
  • 17
  • Could you explain in more detail your usecase? From your current explanation I conclude following: This seems very expensive, for every write you would pay for each of the documents that you retrieve. If NAME is unique than you can name documents using is at unique identifier. Than you could just solve your access rights through CREATE / UPDATE. – radulle Jan 10 '20 at 14:19

1 Answers1

0

There is no way to search a collection for a specific value in security rules, as that operation wouldn't scale.

If you want to ensure unique user names, you'll have to create a collection where you use the user names as the key. You can then use the exists function in your security rules, to check if the name already exists in the collection.

Also see:

Edric
  • 24,639
  • 13
  • 81
  • 91
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807