gpt4 book ai didi

javascript - 使用 firebase 数据库规则无法获取响应数据

转载 作者:行者123 更新时间:2023-12-02 22:22:44 27 4
gpt4 key购买 nike

为我的报价表添加规则后,我没有从 Firebase 实时数据库获得任何返回。我希望返回 auth.uid 与报价的所有者值匹配的所有报价。然而我什么也没得到。

谁能帮帮我吗?

规则指定为:

{
"rules": {
"users":{
".read": "auth.uid != null",
".write": "auth.uid != null"
},
"quotes":{
"$uid":{
".read": "data.child('owner').val() === auth.uid",
}
}
}
}

报价表的结构如下:

{
"1ec658d2-a7cb-45b8-8d9b-9c2a6783365d" : {
"dateCreated" : "2019-12-02T16:06:50+01:00",
"owner" : "DVRVSpeOXQV6wAmHAdpAe6iPQ5i2",
"ownerName" : "testOost",
"projectName" : "testProject1"
},
"96549b51-6356-4c37-a388-592561394d1a" : {
"dateCreated" : "2019-09-25T14:58:13+02:00",
"owner" : "xZBFCq4ho3V2G8dZTvK7RjsnTr43",
"ownerName" : "timcastelijn",
"projectName" : "testProject2"
}
}

我访问数据的代码是

this.props.firebase.db.ref('quotes/').on('value', snapshot => {
const quotesObject = snapshot.val();

/* quotesObject is handled here */

});

更新:已解决

该问题已通过 @frank-van-puffelen 的解决方案解决。

新规则:

{
"rules": {
"users":{
".read": "auth.uid != null",
".write": "auth.uid != null"
},
"quotes":{
".read": "auth.uid != null &&
query.orderByChild == 'owner' &&
query.equalTo == auth.uid" // restrict basket access to owner of quote
}
}
}
}

新片段:

this.props.firebase.db.ref('quotes/').orderByChild("owner")
.equalTo(this.authUser.uid)
.on("value", snapshot => {

const quotesObject = snapshot.val();

....

});

}

最佳答案

您的代码尝试从 /quotes 读取,但您的规则不允许任何人从 /quotes 读取。所以规则拒绝读取操作。

请记住:

  • 规则不会过滤数据,而只是强制执行访问规则。
  • 如果您只想允许用户读取自己的数据,则您的代码和规则需要协同工作才能实现这一点。

在您当前的用例中,您只需修改代码以仅读取该特定用户的节点:

var uid = firebase.auth().currentUser.uid;
this.props.firebase.db.ref('quotes/'+uid).on('value', snapshot => {
const quotesObject = snapshot.val();

/* quotesObject is handled here */

});

在其他情况下,您需要在代码中使用查询,然后在规则中验证该查询。

有关这些的更多信息,请参阅:

关于javascript - 使用 firebase 数据库规则无法获取响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59179027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com