检索Firebase数据库时,子节点值为空

时间:2018-09-22 18:20:28

标签: java android firebase firebase-realtime-database

我正在尝试检索数据库值,但是使用uID提取的节点/子节点中的数据为空。     这是我的代码-

    Intent intent = getIntent();
            Bundle extras = intent.getExtras();
            currentUser = extras.getString("currentUser");
            selectedUser = extras.getString("selectedUser");
            uID = extras.getString("uID");
    mFirebaseDatabase = FirebaseDatabase.getInstance();


mDbReference = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference uidRef = mDbReference.child("users").child(uID);

    ValueEventListener valueEventListener = new ValueEventListener() {


@Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                ChatMessageModel chatMessageModel = ds.getValue(ChatMessageModel.class);
                Log.d(TAG, "onDataChange: " + uidRef);
                Log.d("users:: ", chatMessageModel.getMsgContent());
                    messageList.add(chatMessageModel);
                    for (int i = 0; i < messageList.size(); i++) {
                        Log.d(TAG, "All received messages : " + messageList.get(i).getMsgContent());

                    }
                }
                chatAppMsgAdapter.notifyDataSetChanged();
                //Do what you need to do with the list.
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d(TAG, databaseError.getMessage());
            }
        };
        uidRef.addListenerForSingleValueEvent(valueEventListener);

//这是我的数据库
 u0TddklO8BXKCRHp4UFXEvmPS1H3:null // u0TddklO8BXKCRHp4UFXEvmPS1H3是uid。 uid是当前用户。

2 个答案:

答案 0 :(得分:0)

假设user1是第一个用户的对应uid,要使所有ChatMessageModel对象在您的user1节点下,请使用以下代码:< / p>

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ArrayList<ChatMessageModel> messageList = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            ChatMessageModel chatMessageModel = ds.getValue(ChatMessageModel.class);
            Log.d("users:: ", chatMessageModel.getMsgContent());
            messageList.add(chatMessageModel);
        }

        //Do what you need to do with the list.
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

答案 1 :(得分:-1)

  1. 首先尝试通过将Firebase数据库规则设置为以下内容来调试应用程序-

    {
       "rules": {
       ".read": true,
       ".write": true
       }
    } 
    
  2. 如果仍然无效,请尝试执行此操作-

    private FirebaseDatabase database;
    private DatabaseReference databaseRef;
    
    database = FirebaseDatabase.getInstance();
    databaseRef = database.getReference("users");
    
    databaseRef.addValueEventListener(new ValueEventListener() {
         @Override
         public void onDataChange(DataSnapshot dataSnapshot) {
             for(DataSnapshot snapshot:dataSnapshot.getChildren()){
                Log.d("Output Key", snapshot.getKey());
                Log.d("Output Value", snapshot.getValue().toString());
            }
         }
         @Override
         public void onCancelled(DatabaseError databaseError) {
    
         }
    });