Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 即使安全规则拒绝创建对象,添加子级的Firebase也能正常工作_Javascript_Firebase_Firebase Realtime Database_Firebase Security - Fatal编程技术网

Javascript 即使安全规则拒绝创建对象,添加子级的Firebase也能正常工作

Javascript 即使安全规则拒绝创建对象,添加子级的Firebase也能正常工作,javascript,firebase,firebase-realtime-database,firebase-security,Javascript,Firebase,Firebase Realtime Database,Firebase Security,我不知道怎么可能,但即使是安全规则也会拒绝新数据,因为它会在我的视图中添加新对象。我的发言如下: dbRef = db.ref('/PostsCompany/' + companyKey + '/Posts').orderByChild('Date'); dbRef.on('child_added', function (snap) { console.log(snap.val()); }) 数据库结构: { "PostsCompany" : { "-L3Y7zO

我不知道怎么可能,但即使是安全规则也会拒绝新数据,因为它会在我的视图中添加新对象。我的发言如下:

dbRef = db.ref('/PostsCompany/' + companyKey + '/Posts').orderByChild('Date');
dbRef.on('child_added', function (snap) {
   console.log(snap.val());
})
数据库结构:

  {
    "PostsCompany" : {
      "-L3Y7zOHpKu0tbr-cyXQ" : {
        "LastPost" : {
          "CommentsEnabled" : true,
          "CompanyKey" : "-L3Y7zOHpKu0tbr-cyXQ",
          "Date" : 1517316150394,
          "Message" : "test msg",
          "PostKey" : "-L464lDsQc8wmpuynK_n",
          "Type" : "Post"
        },
        "Posts" : {
          "-L464lDsQc8wmpuynK_n" : {
            "CommentsEnabled" : true,
            "CompanyKey" : "-L3Y7zOHpKu0tbr-cyXQ",
            "Date" : 1517316150394,
            "Message" : "test msg",
            "Type" : "Post"
          }
        }
      }
    }
  }
相关规则:

"PostsCompany": {
  "$companyKey": {
    ".read": "auth !== null",
    "Posts": {
        ".indexOn": ["Date"],
        "$postKey": {
            ".write": "auth.uid == root.child('CompanyPages')
                                       .child($companyKey)
                                       .child('OwnerUID')
                                       .val()",
            ".validate": "root.child('CompanyPages')
                              .child($companyKey)
                              .exists() &&
                          newData.hasChildren(['CompanyKey','Message','Date','Type','CommentsEnabled'])",
            "CompanyKey": {
                ".validate": "!data.exists() && 
                                        newData.val() == $companyKey"
            },
            "Date": {
                ".validate": "!data.exists() && 
                                        newData.val() <= now"
            },
            "Message": {
                ".validate": "!data.exists() && 
                                        newData.isString() && 
                                        newData.val().length<=1000"
            },
            "Type": {
                ".validate": "!data.exists() && 
                                        newData.val() == 'Post'"
            },
            "Media": {
                ".validate": "!data.exists() && 
                                        newData.hasChildren(['Type','Preview'])",
                "Type" : {
                    ".validate": "newData.val() == 'Image'"
                },
                "Preview" : {
                    ".validate": "newData.isString()"
                },
                "$other": {
                  ".validate": false
                }
            },
            "CommentsEnabled": {
                ".validate": "newData.isBoolean()"
            },
            "Comments": {
                ".read": " auth !== null",
                "$commentKey": {
                    ".write": " auth !== null &&
                                root.child('PostsCompany')
                                  .child($companyKey)
                                                .child('Posts')
                                  .child($postKey)
                                  .child('CommentsEnabled')
                                  .val() == true",
                    ".validate": "newData.hasChildren(['UID','Message','Date'])",
                    "UID": {
                        ".validate": "!data.exists() && 
                                      auth.uid == newData.val()"
                    },
                    "Date": {
                        ".validate": "!data.exists() && 
                                      newData.val() <= now"
                    },
                    "Message": {
                        ".validate": "!data.exists() && 
                                      newData.isString() && 
                                      newData.val().length<=1000"
                    },
                    "Likes": {
                        "$uidCommentLike": {
                            ".write": "auth.uid == $uidCommentLike",
                            ".validate": "newData.isBoolean()"
                        }
                    },
                    "Replies": {
                        "$replyKey": {
                            ".validate": "newData.hasChildren(['UID','Message','Date'])",
                            "UID": {
                                ".validate": "!data.exists() && 
                                                            auth.uid == newData.val()"
                            },
                            "Date": {
                                ".validate": "!data.exists() && 
                                              newData.val() <= now"
                            },
                            "Message": {
                                ".validate": "!data.exists() && 
                                              newData.isString() && 
                                              newData.val().length<=1000"
                            },
                            "Likes": {
                                "$uidReplyLike": {
                                    ".write": "auth.uid == $uidReplyLike",
                                    ".validate": "newData.isBoolean()"
                                }
                            },
                            "$other": {
                              ".validate": false
                            }
                            }
                    },
                    "$other": {
                        ".validate": false
                    }
                }
            },
            "Likes": {
                "$uid": {
                    ".write": "auth.uid == $uid",
                    ".validate": "newData.isBoolean()"
                }
            },
            "$other": {
              ".validate": false
            }
        }
    }
  }      
},

原因是Firebase执行的乐观更新。在与谷歌的数据库服务器vesion同步之前,它在本地版本的数据库中添加了新的post


连接到Firebase数据库的每个客户端都维护其自己的任何活动数据的内部版本。写入数据时,首先将其写入本地版本。然后,Firebase客户端将该数据与远程数据库服务器以及“尽最大努力”与其他客户端同步

见:


child\u added
发生在规则执行之前,这就是为什么它在您的案例中运行,而您无法同时在Firebase控制台中看到结果


如果您定义
child\u removed
,您将看到它在创建新帖子后立即被调用。

请发布您的安全规则和数据库结构。。。我们可能还需要查看
companyKey
的值。我编辑了问题并添加了完整的详细信息。您的代码和数据库结构显示节点
CompanyPosts
。但是你的安全规则没有。他们有一个
PostsCompany
节点。这是我在这里写的错误。更正为“PostsCompany”
newData.hasChildren(['CompanyKey','Message','Date','Type','CommentsEnabled'])