简体   繁体   English

通过Firebase实时数据库中的子节点获取父节点

[英]Getting parent node through child node in Firebase Realtime database

In my app in Android studio I've coded for allowing users to book appointment with doctors.在我在 Android 工作室的应用程序中,我编写了允许用户预约医生的代码。 So if the doctor accepts the appointment, I want to change the value of child node "Status".所以如果医生接受预约,我想改变子节点“状态”的值。 For that I need the value of the parent node which is为此,我需要父节点的值,即这个 . .

For that I tried using String currentUserId = ds.getKey();为此,我尝试使用String currentUserId = ds.getKey(); ,but ds,getKey() gives the value "Patient" which is a child node of the required parent node. ,但是 ds,getKey() 给出的值是“Patient”,它是所需父节点的子节点。 So how do I get the key (circled red in image)?那么我如何获得钥匙(图中红色圆圈)?

patRef = database.getReference().child("Patient Appointments");

patRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String pat = ds.child("Patient").getValue(String.class);

                if(pat.equals(Email)){
                    HashMap update= new HashMap();
                    update.put("Status","Appointment on "+date);

                    String currentUserId = ds.getKey();
                    Toast.makeText(DoctorNotification.this, currentUserId, Toast.LENGTH_SHORT).show();

                    patRef.child(currentUserId).updateChildren(update).addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public void onSuccess(Object o) {
                            Toast.makeText(DoctorNotification.this, "Appointment accepted", Toast.LENGTH_SHORT).show();
                        }
                    });

                    flag1="True";
                }

            }

            if(flag1.equals("False")){
                Toast.makeText(DoctorNotification.this, "Error", Toast.LENGTH_SHORT).show();

            }
        }

    }
});

As you discovered yourself, you can update the current node in the loop with:正如您自己发现的那样,您可以使用以下命令更新循环中的当前节点:

ds.getRef().child(currentUserId).updateChildren(update)

I wanted to provide an additional comment on the approach you take to finding the node to update.我想就您查找要更新的节点所采取的方法提供额外的评论。 Right now you load all Patient Appointments to find one of them, which is going to be inefficient - especially one you start adding more appointment.现在,您加载所有Patient Appointments以找到其中一个,这将是低效的 - 尤其是您开始添加更多预约的那个。

Use a query to only load the node for the current patient使用查询仅加载当前患者的节点

One way to improve this is by using a query to only retrieve the appointment of the Patient you're looking for.改善这一点的一种方法是使用查询来仅检索您正在寻找的Patient的约会。 That'd look something like this:看起来像这样:

patRef = database.getReference().child("Patient Appointments");

Query patientQuery = patRef.orderByChild("Patient").equalTo(Email);

patientQuery.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    ...

And you can then remove the if(pat.equals(Email)){ from within the onComplete .然后您可以从onComplete中删除if(pat.equals(Email)){

Use a query on both Patient and Status fields对 Patient 和 Status 字段使用查询

One smaller optimization after this is that you probably only need to check the appointments that are pending.在此之后的一个较小的优化是您可能只需要检查待处理的约会。 For that you'd like a query like above, but then on two fields.为此,您想要一个像上面这样的查询,但是在两个字段上。

This is not something the Firebase Realtime Database natively supports, but you can emulate it by adding a composite field to your data: "Patient_Status": "Elise@doc.com_Pending" .这不是 Firebase 实时数据库本身支持的东西,但您可以通过向数据添加复合字段来模拟它: "Patient_Status": "Elise@doc.com_Pending" The query would then become:然后查询将变为:

patRef.orderByChild("Patient_Status").equalTo(Email+"_Pending");

Store the key of the node in your UI将节点的密钥存储在您的 UI 中

The final improvement assumes that the doctor is actually shown a list of all (pending) appointments and then click to accept one or more of them.最后的改进假设医生实际上看到了所有(待定)约会的列表,然后单击以接受其中的一个或多个。 In such a scenario you already read all the appointments from the database to show them in the user interface.在这种情况下,您已经从数据库中读取了所有约会以在用户界面中显示它们。

If you store the key of each appointment in a (hidden) field in the user interface, you can then use that key when the doctor clicks it, and the entire update operation can become as simple as:如果您将每个约会的密钥存储在用户界面的(隐藏)字段中,那么您可以在医生单击时使用该密钥,整个更新操作可以变得如此简单:

patRef
  .child("keyOfTheAppointmentTheDoctorClicked")
  .child("Status")
  .setValue("Appointment on "+date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问子节点 Firebase 实时数据库 Swift - Accessing Child Node Firebase Realtime Database Swift React Native:在 firebase 实时数据库中创建自定义子节点 - React Native: Create custom child node in firebase realtime database 如何用key删除firebase实时数据库子节点? - how to delete firebase realtime database child node with key? 连接到用户的唯一节点 - Firebase 实时数据库 - Unique node connected to user - Firebase Realtime Database 如何删除firebase实时数据库中的一个节点javascript - How to delete a node in firebase realtime database in javascript Firebase Realtime Database随机选择子节点后如何获取子节点的key - How to get the key of a child node in Firebase Realtime Database after choosing a child randomly 计算 Firebase 实时数据库中节点的大小 - Calculating the size of a node in Firebase Realtime Database 如何从firebase实时数据库中删除选中的item节点? - How to delete selected item node from firebase realtime database? 将新属性更新到现有节点 firebase(实时数据库)(编辑) - update new attribute into exists node firebase(realtime database) (edit) 从 Node.js 中的 Firebase 实时数据库中选择特定属性 - Select specific properties from Firebase Realtime Database in Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM