我使用 Redux-Saga 作为中间件。我通过查询将参数传递给 Firebase 数据库,但无法在数据库端访问它。
Query:::
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:::::
"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" : "[email protected] /cdn-cgi/l/email-protection",
"role" : "user",
"uid" : "6OiasllKwVSjjRfrCarMAjhkKAH2"
}
}
非常感谢任何形式的帮助。
提前非常感谢您。
编辑:::: 添加了管理员查询。我想在管理员的情况下获取集合中的所有可用数据。