[英]How can I query in firebase android like database trigger in cloud function
In Firebase Cloud Functions (Javascript), we use database trigger like below,在 Firebase Cloud Functions (Javascript) 中,我们使用如下数据库触发器,
functions.database.ref('/DoctorApp/DrTimeSlot/{doctorUID}/Patients/{patientID}')
.onCreate((snapshot, context) => {
// codes here
};
Here, {doctorUID} is not known so all function is working on all UIDs.此处,{doctorUID} 未知,因此所有函数都适用于所有 UID。
Is this possible to use the same method in Android Studio (Java)?这可以在 Android Studio (Java) 中使用相同的方法吗? below this code is not working for me, can you provide me the alternate of the code.
下面这段代码对我不起作用,你能给我提供代码的替代品吗?
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) {
}
});
In android you cannot do this query child("DrTimeSlot/{drUID}/Patients/")
, all 3 nodes need to be known.在android中,您不能执行此查询
child("DrTimeSlot/{drUID}/Patients/")
,需要知道所有 3 个节点。
If you don't know the drUID
, then when you stored it first to the database, you can store it to a variable and pass the variable around, example:如果您不知道
drUID
,那么当您首先将其存储到数据库时,您可以将其存储到一个变量中并传递该变量,例如:
String drUID = ref.push().getKey();
Then you can pass the above variable using intent to different activities.然后您可以使用意图将上述变量传递给不同的活动。
While not possible to use the same syntax as Cloud Functions, your query to find all doctors with a given patient ID can be achieved using:虽然无法使用与 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());
}
});
However, to permit such an operation, the client must have read access to every single doctor's data , even if the user performing the search is not a patient of that doctor.但是,要允许这样的操作,客户端必须对每个医生的数据具有读取权限,即使执行搜索的用户不是该医生的患者。 It will also be entirely processed on the client device which means downloading all of the data under
"DrTimeSlot"
which is not recommended and is also a massive security & privacy risk .它也将在客户端设备上完全处理,这意味着下载
"DrTimeSlot"
下的所有数据,这是不推荐的,也是巨大的安全和隐私风险。
Instead, in the user data of a particular user, store a list of their doctors.相反,在特定用户的用户数据中,存储他们的医生列表。 When you need to find them, download
/users/userId1/doctors
and then get the needed patient data from each doctor restricting read/write access to that doctor and that user only .当您需要查找它们时,请下载
/users/userId1/doctors
,然后从每个医生那里获取所需的患者数据,限制对该医生和该用户的读/写访问权限。
"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
},
...
},
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.