获取与另一个子项具有相同父项的特定子项 Firebase。

huangapple 未分类评论46阅读模式
英文:

Get specific child with same parent as another child Firebase

问题

我正在使用Android Studio制作Android应用程序,希望从Firebase获取特定数据。我正在尝试通过Firebase实时数据库搜索所有具有usercode1234的用户,然后获取他们的名称。我在Firebase中的设置如下:

Firebase设置图片链接

由于我正在搜索usercode = 1234的数据,它将在数据快照中返回有关Joe和Lily的所有信息(因为他们是具有usercode 1234的两个用户),但不应返回有关Kevin的信息。这是我如何设置的:

Query getUsers = ref.orderByChild("usercode").equalTo(1234);
getUsers.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()){
            // 具有usercode = 1234的用户存在
        } else {
            // 具有该usercode的用户不存在
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});
}

如果将datasnapshot转换为字符串,我认为将包含所有Lily和Joe的信息。
我想做的是从那个datasnaphot中获取第一个用户的name(在这种情况下是Joe)。然后,我的代码将在通过一些测试之前使用它,然后转到第二个用户的name(这将是Lily),并重复测试。另外,如果有100个用户,那么对于这100个具有usercode1234的用户,我需要重复这个过程,所以我可能需要一些可重复的东西。

因此,我需要找到具有usercode1234的所有用户的子项name的第一个值,然后重复,直到获取所有名称并对所有名称运行测试。

我刚刚开始使用Stackoverflow,请告诉我是否缺少任何有助于回答问题的信息。

---- 编辑 ----

我现在已经找出如何在Firebase Firestore中实现此目标。它具有更简单的查询方式,并允许您对结果进行更多操作。如果其他人遇到我上面提到的相同问题,Firebase Firestore可能是您想要尝试的内容。

英文:

I am making an Android app in Android Studio and I want to get specific data from the Firebase. I am trying to search through the Firebase Realtime Database to get all of the Users that have a usercode of 1234 and then getting their name. This is how I have it set up in Firebase:

Firebase Setup image link

Since I am searching for the data where the usercode = 1234 it will return (in the datasnapshot) all of the information about Joe and Lily (since they were the two users that had a usercode of 1234), but it shouldn't return information about Kevin. Here is how I did that:

Query getUsers = ref.orderByChild("usercode").equalTo(1234);
    getUsers.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                //User with usercode = 1234 exists
            } else {
                //User with that Usercode doesn't exist
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

The datasnapshot, if converted to a string would have all of Lily and Joe's information I believe.
What I want to do is get the first user's name (Which would be Joe in this case) from that datasnaphot. My code will then use it through a few tests before going to the second user's name(Which would be Lily) and repeating the tests. Also, if there were 100 Users then I would need to repeat it for every one of those 100 that had a user code of 1234, so I probably need something repeatable.

So somehow I need to find the first value of the child's name from all of the Users that have a usercode of 1234 and then repeat until I get all of the names and run the test on all of them.

I am new to Stackoverflow, so please tell me if I am missing any information that would be helpful in answering the question.

---- EDIT ----

I have now figured out how to do this with Firebase Firestore. It has simpler queries and allows you to do more with the results. If anyone else has the same issue that I stated above, Firebase Firestore is something you might want to try out.

答案1

得分: 0

使用以下代码行时:

Query getUsers = ref.orderByChild("usercode").equalTo("1234");

您正在搜索 Users 节点中所有具有 usercode 属性设置为 "1234" 的用户。这个查询将始终不会返回结果,因为您正在将 1234 作为字符串进行搜索,而不是作为数字,就像它在数据库中存储的那样。正确的查询应该是:

Query getUsers = ref.orderByChild("usercode").equalTo(1234); // 不要使用双引号
英文:

When using the following line of code:

Query getUsers = ref.orderByChild("usercode").equalTo("1234");

You are searching through the Users node, all users that have the usercode property set to "1234". This query will always return no results because you are searching for 1234 as a String and not as a number, as it is stored in the database. The correct query should be:

Query getUsers = ref.orderByChild("usercode").equalTo(1234); //No double quotes

huangapple
  • 本文由 发表于 2020年7月24日 22:43:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/63075937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定