模拟器拒绝将新数据设置为Firebase数据库

时间:2018-10-30 21:43:02

标签: firebase firebase-realtime-database firebase-security firebase-security-rules

我正在为我的Firebase数据库调整规则。当我尝试向数据库中设置新数据时,我总是被拒绝。这是我数据库的规则:

 {
  /* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
  "rules": {

    "challenges":{
      ".write": "auth != null",
      //".write":"auth.uid == \"DFhNb28506Y345CpJ3Ye7DQNn713\" || ((newData.exists() && !data.exists()) || (auth.uid.value == data.child(\"player1Uid\").val() || auth.uid.value == data.child(\"player2Uid\").val()))",
      ".read": "auth != null"
    },

    "comments":{
        ".write": "auth != null",
//      ".write":"auth.uid == \"DFhNb28506Y345CpJ3Ye7DQNn713\" || ((newData.exists() && !data.exists()) || auth.uid.value == data.child(\"writerUid\").val())",
      ".read": "auth != null"
    },

    "lessons":{
       ".write": "auth != null",
      //".write":"auth.uid == \"DFhNb28506Y345CpJ3Ye7DQNn713\" || ((newData.exists() && !data.exists()) || auth.uid.value == data.child(\"writerUid\").val())",
      ".read": "auth != null"
    },

    "posts":{
      ".read": "auth != null",
      //".write": "auth != null"

         "$postId": {
        // a new post can be created if it does not exist
        // existing posts can only be edited by their original "author"
        ".write": "(!data.exists() && newData.exists() && auth != null) || data.child('writerUid').val() == auth.uid || auth.uid == \"DFhNb28506Y345CpJ3Ye7DQNn713\" "
        //".validate": "newData.hasChildren(['title', 'author', 'timestamp'])",
     }
    },

    "questions":{
      ".write": "auth != null",
      //".write":"auth.uid == \"DFhNb28506Y345CpJ3Ye7DQNn713\" || ((newData.exists() && !data.exists()) || auth.uid.value == data.child(\"writerUid\").val())",
      ".read": "auth != null"
    }, 

    "users":{
      ".write": "auth != null",
      //".write":"auth.uid == \"DFhNb28506Y345CpJ3Ye7DQNn713\" || ((newData.exists() && data.child(\"userEmail\").val() != null) || auth.token.email == data.child(\"userEmail\").val())",
      ".read": "auth != null"
    },

  }
}

当我尝试添加新帖子时,如果我尝试使用我在应用程序中对其进行硬编码编写的uid添加新帖子,也总是会拒绝该帖子。我花了很长时间尝试设置规则,但直到现在我还不能设置它,我发现规则无法调试,因此希望有人能帮助我解决这个问题。

这是我的数据库结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须取消注释此行

      //    "$postId": {

您在节点“ posts”中已经有帖子,因此!data.exists返回false并阻止您的新帖子