I want to write a rule that will don't allow add same document second time

后端 未结 2 1271
醉酒成梦
醉酒成梦 2021-01-28 03:54
rules_version = \'2\';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{usersID} {
    allow read;
    allow write: if request.auth.         


        
相关标签:
2条回答
  • 2021-01-28 04:17

    Here's how I avoid duplication for tokens You can do it for any field.

    First query through document and if null then add that collection.

    @override
      void initState() {
        // TODO: implement initState
        super.initState();
        firebaseMessaging.getToken().then((token){
          print(token);
          bool isAvailabe;
          Firestore.instance.collection("tokens").where("token",isEqualTo: token).getDocuments().then((snap){
            if(snap == null){
              Firestore.instance.collection('tokens').add({"token":token}).catchError((error){
                print(error);
              }).catchError((e){
                print(e);
              });
            }
          });
    
        });
    
      }
    
    0 讨论(0)
  • 2021-01-28 04:22

    What you are trying to do is not possible with security rules. This would require that you be able to perform a query on desksCollection for documents that match the criteria of having a field with a certain value. However, performing queries is not possible in security rules. You can fetch a document by its ID, but that won't help you here, because you don't know which document to fetch.

    If you require that deskName be unique, then consider using that value as the ID of the document. Then you can write a rule to prevent updates if the document already exists.

    0 讨论(0)
提交回复
热议问题