简体   繁体   English

我如何在firebase android中查询像云函数中的数据库触发器

[英]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.

相关问题 如何在 Android 的 firebase 数据库中存储 FireBase 云消息传递令牌? - How can I store FireBase Cloud Messaging tokens in firebase database in Android? 如何使用不同的 Firebase 数据库名称查询数据? - How can I query data with different firebase database names? 如何在Android Firebase数据库发布中添加更多参数? - How can I add more parameters in Android Firebase database post? 当标记Android Studio触发时,如何获取保存在Firebase中的数据 - How can i get data latlng that i saved in firebase when it trigger by marker android studio Firebase Cloud Function onWrite完成后,如何触发Android客户端回调? - How do I get Android client side callback to fire after Firebase Cloud Function onWrite finishes? 如何在 Firebase 数据库中注册和存储数据 - Android Studio | Firebase - How can I Register and Store Data in Firebase Database - Android Studio | Firebase How can I get full URL (like below) from direct Firebase Storage for Android JAVA OR Kotlin - How can I get full URL (like below) from direct Firebase Storage for Android JAVA OR Kotlin 使用MaterialSearchBar的Firebase数据库参考的SQL类似查询 - Sql Like query for Firebase Database Reference with MaterialSearchBar 像Android SDK的Appcelerator云服务中的查询 - like query in Appcelerator cloud service for android sdk 我想使用 PHP 开发一个 Android 聊天应用程序。 我该怎么做? 不使用 Google Firebase Cloud Messeging 和 AWS - I want to develop an Android chat app using PHP. How can I do it? without using Google Firebase Cloud Messeging and AWS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM