gpt4 book ai didi

java - 如何在 firebase android 中查询类似云函数中的数据库触发器

转载 作者:行者123 更新时间:2023-12-01 19:24:33 24 4
gpt4 key购买 nike

在 Firebase Cloud Functions (Javascript) 中,我们使用如下所示的数据库触发器,

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) {

}
});

最佳答案

虽然无法使用与 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,然后从限制对 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
},
...
},
...
}

关于java - 如何在 firebase android 中查询类似云函数中的数据库触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59323701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com