简体   繁体   English

Firebase返回空值

[英]Firebase return null value

This is the structure: 结构如下:

在此处输入图片说明

I wanna select the company, phone and city where the email = current email of the connected user. 我想选择公司,电话和城市,其中电子邮件=已连接用户的当前电子邮件。

This is my code: 这是我的代码:

DatabaseReference mDatabases = FirebaseDatabase.getInstance().getReference();

Query query = mDatabases.child("Users").orderByChild("email").equalTo(FirebaseAuth.getInstance().getCurrentUser().getEmail().toString());

query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {


            User user =  dataSnapshot.getValue(User.class);
            String company = user.getCompany();
            String city = user.getCity);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

The returned result is null. 返回的结果为空。

When i change dataSnapshot.getValue(User.class) to dataSnapshot.getValue() , it return all values of the node. 当我将dataSnapshot.getValue(User.class)更改为dataSnapshot.getValue() ,它将返回该节点的所有值。

You haven't indicated which user you want to retrieve. 您尚未指明要检索的用户。 As your dataSnapshot returns a list of user values (even the list has only 1 element), you have to specify which user you want to get: 当dataSnapshot返回用户值列表时(即使列表中只有1个元素),您必须指定要获取的用户:

User user =  dataSnapshot.child(userId).getValue(User.class);

Replace userId to the id of the user you want to retrieve (current user I guess!) userId替换为您要检索的用户的ID(我想是当前用户!)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM