我正在使用Redux-Saga作为中间件 . 我通过查询将参数传递给Firebase数据库,但无法在数据库端访问它 .
查询:::
database.ref("workouts")
.child(userId)
.once("value")
.then(snapshot => {
console.log("onSuccess:", snapshot.ref, snapshot.val());
resolve(snapshot.val());
})
.catch(function(error) {
console.log("Error fetching document: ", error);
reject(error);
});
UserId是我从localStorage获取并通过查询使用“.child(userId)”发送到数据库的值
查询:::(适用于管理员)
database.ref("workouts")
.once("value")
.then(snapshot => {
console.log("onSuccess:", snapshot.ref, snapshot.val());
resolve(snapshot.val());
})
.catch(function(error) {
console.log("Error fetching document: ", error);
reject(error);
});
数据库中的规则::::
{
"rules": {
"workouts": {
// grants write access to the owner of this user account || the user role is equal to admin
// grants read access to the owner of this user account || the user role is equal to admin
".read":"(data.exists() && auth.uid != null && data.child(auth.uid).exists()) ||root.child('users').child(auth.uid).child('role').val() == 'admin'",
".write":"data.exists() ||root.child('users').child(auth.uid).child('role').val() == 'admin'"
}
}
}
我已经尝试[query.equalTo]和[data.child(auth.uid).val()]方法来访问该值,但没有得到任何结果 .
JSON for Workouts :::::
"workouts" : {
"6OiasllKwVSjjRfrCarMAjhkKAH2" : {
"-LD3nNIKw9Yk3HcoAL0-" : {
"exercises" : [ {
"muscleGroup" : "Chest",
"name" : "Incline Dumbbell Fly",
"sets" : [ 0, 0, 0, 0, 0 ],
"type" : "reps"
} ],
"name" : "Force Set",
"reps" : [ "5", "5", "5", "5", "5" ],
"type" : "Weights"
}]
},
"workoutName" : "My Test workout"
}
用户JSON :::::
"users" : {
"6OiasllKwVSjjRfrCarMAjhkKAH2" : {
"email" : "testuser@gmail.com",
"role" : "user",
"uid" : "6OiasllKwVSjjRfrCarMAjhkKAH2"
}
}
任何形式的帮助都非常感谢 .
非常感谢你提前 .
编辑::::添加了admin的查询 . 我想在admin的情况下获取集合中的所有可用数据 .
1 回答
我想我看到了什么问题 . 您的JSON似乎在
/workouts/$uid
下为用户提供了所有锻炼 . 您的规则尝试授予用户访问/workouts
的所有权限,而不仅仅是他们自己的访问权限 .解决方案是将规则向下移动到树中一级:
另请参阅documentation on securing user data,其中有一个很好的简单示例 .
Update :如果您想让管理员阅读
/workouts
并且每个用户都能够在/workouts/$uid
下阅读他们自己的锻炼,那么您需要以下规则: