Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How check if field exists with firestore rules?

I have a firestore database with a collection of products and a collection of categories.

I want give at the user the delete permission on the categories collection only if the category document not have a products field.

the products field is an array of reference.

I have this rules:

match /shops/{shopId} {
  allow read: if true;
  allow write: if false;
  match /categories/{category=**}{      
    allow read: if true;
    allow write: if resource.data.products == null;
  }
}

But this rules not works, I cannot write or update a document.

like image 209
SaroVin Avatar asked May 22 '20 10:05

SaroVin


People also ask

How do I check my firestore rules?

Open the Firebase console and select your project. Then, from the product navigation, do one of the following: Select Realtime Database, Cloud Firestore, or Storage, as appropriate, then click Rules to navigate to the Rules editor.

Does firestore Create collection if not exists?

Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.


1 Answers

Try this

match /shops/{shopId} {
  allow read: if true;
  allow write: if false;
  match /categories/{category=**}{      
    allow read: if true;
    allow create: if true;
    allow update, delete: if !('products' in resource.data);
  }
}
like image 161
l1b3rty Avatar answered Nov 07 '22 23:11

l1b3rty