【问题标题】:firebase rules, owner can deletefirebase 规则,所有者可以删除
【发布时间】:2018-04-21 03:51:14
【问题描述】:

我是 Firebase 的新手。我正在尝试设置规则,并且我正在使用此规则来让只有用户评论所有者才能编写。问题是它需要发布者 ID(所有者 ID),但是当我尝试删除节点时它不起作用,因为我无法发送发布者 ID(可以吗?)。这是规则:

"infinity_comments":{
    "country": {
         "$countryid": {
            "$postid":  {
               "$pushid": {
                    ".write": "(!data.exists() && newData.child('posterid').val() == auth.uid ) || (data.child('posterid').val() == auth.uid && newData.child('posterid').val() == auth.uid)"
                 } 
            }
        }
    }
},

这就是我尝试删除评论的方式:

firebase.database().ref('infinity_comments/country/countryid/postid/pushid/').remove(function(error){
    if (!error) {
        alert('success');
    }
    else {
        alert(error)
    }
})

我收到权限被拒绝错误。

【问题讨论】:

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


    【解决方案1】:

    您的权限被拒绝,因为删除实际上是一个具有空值的写入操作。您可以更新您的规则,添加一个新的 OR 子句,如下所示:

    ".write": "(!data.exists() && newData.child('posterid').val() == auth.uid )
    || (data.child('posterid').val() == auth.uid && newData.child('posterid').val() == auth.uid)
    || (data.child('posterid').val() == auth.uid && !newData.exists())"
    

    【讨论】:

    • 它不工作,仍然获得权限被拒绝:/
    • 它现在可以工作了,非常感谢,请更正你的代码最后一行,它的 auth.uid。你把它 auth.id 这就是为什么没有工作
    • @SandraLopez,我更正了代码,感谢指出错误!
    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2018-11-27
    • 2020-02-28
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多