我如何在 firebase android 中查询像云函数中的数据库触发器
How can I query in firebase android like database trigger in cloud function
在 Firebase Cloud Functions(Java脚本)中,我们使用如下数据库触发器,
functions.database.ref('/DoctorApp/DrTimeSlot/{doctorUID}/Patients/{patientID}')
.onCreate((snapshot, context) => {
// codes here
};
此处,{doctorUID} 未知,因此所有功能都适用于所有 UID。
是否可以在 Android Studio (Java) 中使用相同的方法?下面这段代码对我不起作用,你能给我提供替代代码吗?
Query query = rootData.child("DrTimeSlot/{drUID}/Patients/").orderByChild("patientUID").equalTo(auth.getUid());
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//codes here
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
在 android 中您不能执行此查询 child("DrTimeSlot/{drUID}/Patients/")
,所有 3 个节点都需要知道。
如果你不知道drUID
,那么你先存到数据库的时候,你可以存到一个变量,然后传给变量,例如:
String drUID = ref.push().getKey();
然后你可以将上面的变量 using intent 传递给不同的活动。
虽然无法使用与 Cloud Functions 相同的语法,但可以使用以下方法实现查找具有给定患者 ID 的所有医生的查询:
Query query = rootData.child("DrTimeSlot").orderByChild("Patients/" + auth.getUid()).startAt(""); // startAt("") will exclude non-existent values
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot queryDataSnapshot) {
if (queryDataSnapshot.hasChildren()) {
// handle no results
System.out.println("No results returned");
return;
}
for (DataSnapshot doctorDataSnapshot: dataSnapshot.getChildren()) {
// TODO: handle the data
System.out.println(doctorDataSnapshot.getKey() + " => " + doctorDataSnapshot.getValue());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
// Getting doctors failed, log a message
Log.w(TAG, "findDoctorsForUser:onCancelled", databaseError.toException());
}
});
但是,要允许这样的操作,客户端必须读取每个医生的数据,即使执行搜索的用户不是该医生的病人。它还将完全在客户端设备上处理,这意味着下载 "DrTimeSlot"
下的所有数据,这是 不推荐的 并且也是 大规模安全和隐私风险.
相反,在特定用户的用户数据中,存储他们的医生列表。当您需要找到它们时,下载 /users/userId1/doctors
然后从限制 read/write 访问 that doctor and that user only.
的每个医生那里获取所需的患者数据
"users": {
"userId1": {
"doctors": {
"doctorId1": true, // boolean value for simplicity
"doctorId2": "paediatrician", // could also use value for type
"doctorId3": 10, // or number of visits
"doctorId4": false, // or if they are an active patient of this doctor
},
...
},
...
}
在 Firebase Cloud Functions(Java脚本)中,我们使用如下数据库触发器,
functions.database.ref('/DoctorApp/DrTimeSlot/{doctorUID}/Patients/{patientID}')
.onCreate((snapshot, context) => {
// codes here
};
此处,{doctorUID} 未知,因此所有功能都适用于所有 UID。
是否可以在 Android Studio (Java) 中使用相同的方法?下面这段代码对我不起作用,你能给我提供替代代码吗?
Query query = rootData.child("DrTimeSlot/{drUID}/Patients/").orderByChild("patientUID").equalTo(auth.getUid());
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//codes here
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
在 android 中您不能执行此查询 child("DrTimeSlot/{drUID}/Patients/")
,所有 3 个节点都需要知道。
如果你不知道drUID
,那么你先存到数据库的时候,你可以存到一个变量,然后传给变量,例如:
String drUID = ref.push().getKey();
然后你可以将上面的变量 using intent 传递给不同的活动。
虽然无法使用与 Cloud Functions 相同的语法,但可以使用以下方法实现查找具有给定患者 ID 的所有医生的查询:
Query query = rootData.child("DrTimeSlot").orderByChild("Patients/" + auth.getUid()).startAt(""); // startAt("") will exclude non-existent values
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot queryDataSnapshot) {
if (queryDataSnapshot.hasChildren()) {
// handle no results
System.out.println("No results returned");
return;
}
for (DataSnapshot doctorDataSnapshot: dataSnapshot.getChildren()) {
// TODO: handle the data
System.out.println(doctorDataSnapshot.getKey() + " => " + doctorDataSnapshot.getValue());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
// Getting doctors failed, log a message
Log.w(TAG, "findDoctorsForUser:onCancelled", databaseError.toException());
}
});
但是,要允许这样的操作,客户端必须读取每个医生的数据,即使执行搜索的用户不是该医生的病人。它还将完全在客户端设备上处理,这意味着下载 "DrTimeSlot"
下的所有数据,这是 不推荐的 并且也是 大规模安全和隐私风险.
相反,在特定用户的用户数据中,存储他们的医生列表。当您需要找到它们时,下载 /users/userId1/doctors
然后从限制 read/write 访问 that doctor and that user only.
"users": {
"userId1": {
"doctors": {
"doctorId1": true, // boolean value for simplicity
"doctorId2": "paediatrician", // could also use value for type
"doctorId3": 10, // or number of visits
"doctorId4": false, // or if they are an active patient of this doctor
},
...
},
...
}