在为我的报价表添加规则后,我没有从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();
....
});
}
答案 0 :(得分:1)
您的代码尝试读取/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 */
});
在其他情况下,您需要在代码中使用查询,然后在规则中验证该查询。
有关这些的更多信息,请参见: