I'm using Redux-Saga as a middle-ware. I'm passing a parameter via query to Firebase Database, but not able to access it on the Database end.
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 is a value I'm fetching from localStorage and sending to database via query using ".child(userId)"
Query::: (For Admin)
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 in database::::
{
"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'"
}
}
}
I've tried [query.equalTo] and [data.child(auth.uid).val()] methods to access the value, but didn't got any result.
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 for users:::::
"users" : {
"6OiasllKwVSjjRfrCarMAjhkKAH2" : {
"email" : "testuser@gmail.com",
"role" : "user",
"uid" : "6OiasllKwVSjjRfrCarMAjhkKAH2"
}
}
Any kind of help is highly appreciated.
Thank you so much in Advance.
Edit:::: Added the query for admin. I want to fetch all the available data in the collection in the case of admin.