以下是我的firebase数据库中的数据,我想获得“医生详细信息”。通过使用他的firebase用户ID为用户。
数据库结构:医生/用户ID /医生详细信息
// Data in Database :
{
"doctors" : {
"65smvSYkhfVBtM2GrPmOl3cdgDy1" : {
"doctorDetails" : {
"designation" : "Principal Scientist",
"docRegNumber" : "242354",
"doctorName" : "Dr Nicole Tesla",
"doctorVerified" : false,
"mobileNumber" : "970050 8070",
"specialization" : "Physicist"
}
},
"FOCSwhfIZSOpqUgueoWlakSkVYr1" : {
"doctorDetails" : {
"designation" : "Principal Scientist",
"docRegNumber" : "87509",
"doctorName" : "Dr Curie ",
"doctorVerified" : false,
"mobileNumber" : "80690 00343",
"specialization" : "Physicist"
}
}
}
}
以下是我用于获取' doctorDetails'通过使用他们的firebase用户ID。
var userId = firebase.auth().currentUser.uid;
firebase.database().ref('doctors/'+userId+'/doctorDetails').on('value', function(snapshot){
console.log(snapshot.val());
});
但是,我仍然无法获得医生的详细信息'对于具有userID的用户,有人可以建议我哪里出错吗?
[编辑1] 上面的代码只监听更改,如何在不附加侦听器的情况下提取数据。
[编辑2] 以下是我设置的权限,我需要进行哪些更改才能读取数据?
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid"
}
},
"doctors":{
"$uid": {
".write": "$uid === auth.uid"
}
}
}
}
答案 0 :(得分:0)
问题在于我的数据库规则:
旧数据库规则
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid"
}
},
"doctors":{
"$uid": {
".write": "$uid === auth.uid"
}
}
}
}
将数据库规则(在databases.rules.json中)更新到下面,这有助于解决问题
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
},
"doctors":{
"$uid": {
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
}
}
}