[英]How to get the key in firebase
I want to retrieve the messages data under a specific key. 我想在特定键下检索消息数据。 But I don't how to get the key. 但是我不知道如何获得钥匙。 Please help, I'm new to firebase. 请帮助,我是Firebase的新手。
In my case right now, I want to get the key encircled below. 就我现在的情况而言,我想在下面圈出它的钥匙。
I have tried this code below but this returns "chat-mates" not the key. 我已经在下面尝试过此代码,但这会返回“聊天伙伴”而不是密钥。
final DatabaseReference ref =FirebaseDatabase.getInstance().getReference().child("chat").child("single-chat").child("converstation").child("chat-mates");
ref.orderByChild("receiverName").equalTo("Liza Soberano").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()){
String key = child.getKey();
Log.e("Key", key);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
I have tried this code below but this returns "chat-mates" not the key. 我已经在下面尝试过此代码,但这会返回“聊天伙伴”而不是密钥。
You must be using DataSnapshot
method to access the JSON tree. 您必须使用DataSnapshot
方法访问JSON树。 The DataSnapshot
element has a method called getKey()
. DataSnapshot
元素具有一个称为getKey()
的方法。 That returns the key of an object. 返回对象的键。
Official Doc: DataSnapShot getKey() method 官方文档: DataSnapShot getKey()方法
Example Code: 示例代码:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot objSnapshot: snapshot.getChildren()) {
Object obj = objSnapshot.getKey();
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("Read failed", firebaseError.getMessage());
}
});
In your case, first get to the child node "conversation" and then apply the above method getKey()
. 在您的情况下,首先进入子节点“对话”,然后应用上述方法getKey()
。
You're building your path wrong and likely end up iterating a different part of the tree, one level above child-mates
. 您正在构建错误的路径,可能最终会遍历树的另一部分,即child-mates
之上的一层。 In that case it would be correct that child-mates
is a child key. 在这种情况下,将child-mates
作为孩子钥匙是正确的。
The problem is in the last child()
call when you create the ref: 问题出在创建引用时的最后一个child()
调用中:
final DatabaseReference ref =FirebaseDatabase.getInstance().getReference()
.child("chat")
.child("single-chat")
.child("converstation")
.child("chat-mates");
There is no child chat-mates
under converstation
, so this ref won't be correct. converstation
下没有儿童chat-mates
,因此该参考不正确。
You probably want to do this: 您可能想这样做:
final DatabaseReference ref =FirebaseDatabase.getInstance().getReference()
.child("chat")
.child("single-chat")
.child("converstation");
ref.orderByChild("chat-mates/receiverName")
.equalTo("Liza Soberano")
.addListenerForSingleValueEvent(new ValueEventListener() {
This will filter on the chat-mates/receiverName
child of each chat. 这将过滤每个聊天的chat-mates/receiverName
子级。
Note that you're going against one of Firebase's recommendations with this data structure. 请注意,此数据结构违反了Firebase的建议之一。 Firebase recommends against nesting data types in the way you do here. Firebase建议不要使用此处的方法来嵌套数据类型。
A more denormalized data model would be: 更加规范化的数据模型将是:
chat-mates
$chatRoomId
receiverName
senderName
chat-messages
$chatRoomId
$messageId
This way you can get the mates/participants in a chat, without accessing (or even needing to have access to) the messages themselves. 这样,您就可以在聊天中吸引伴侣/参与者,而无需访问(甚至无需访问)消息本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.