从.getUid()获取firebase数据库的价值

时间:2022-02-02 20:04:53

I'm working with Android Studio and Firebase and was wondering how I could do the following as there doesn't seem to be much online on how to do this. So I have a table 'users' which contains information about a user and the ID for that table is the users id generated in FirebaseAuth which can be retrieved using .getUid(). I want to query the users table to retrieve some of that data based on the current users id. I would put up some of my code but this is for an assignment. Any pointers would be greatly appreicated. Thanks

我正在使用Android Studio和Firebase,并且想知道如何执行以下操作,因为似乎没有太多关于如何执行此操作的在线。所以我有一个表'users',其中包含有关用户的信息,该表的ID是FirebaseAuth中生成的用户ID,可以使用.getUid()检索。我想查询users表,以根据当前用户ID检索一些数据。我会提出一些代码,但这是为了一项任务。任何指针都会受到很大的影响。谢谢

从.getUid()获取firebase数据库的价值

So I'm wanting to retrieve a value through this table based on EINOwbk5p... so that I'm retrieveing data for the current authorised user.

所以我想基于EINOwbk5p通过这个表检索一个值......这样我就可以为当前的授权用户检索数据了。

2 个解决方案

#1


1  

It's hard to say without seeing code or your data structure, but are you looking at retrieving a db item from the key? If so this might help:

没有看到代码或您的数据结构很难说,但您是否正在寻找从密钥中检索数据库项目?如果是这样可能有所帮助:

private DatabaseReference myRef;
myRef = ....
User getUser = myRef.child(userKey);

getUser.addValueEventListener(new ValueEventListener() {....}

#2


1  

FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
DatabaseReference mFirebaseDbReferenceUserChild = FirebaseDatabase.getInstance().getReference("users");
DatabaseReference mFirebaseDbReferenceCurrentUser= mFirebaseDatabaseReferenceUserChild.child(mFirebaseUser.getUid());
mFirebaseDbReferenceCurrentUser.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            // TO GET USERDETAILS FOR CURRENT USER
            String userDetails = dataSnapshot.getValue().toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

This way you can fetch data of CURRENT USER from "users" table by passing CURRENT USER's uId.

这样,您可以通过传递CURRENT USER的uId从“users”表中获取CURRENT USER的数据。

#1


1  

It's hard to say without seeing code or your data structure, but are you looking at retrieving a db item from the key? If so this might help:

没有看到代码或您的数据结构很难说,但您是否正在寻找从密钥中检索数据库项目?如果是这样可能有所帮助:

private DatabaseReference myRef;
myRef = ....
User getUser = myRef.child(userKey);

getUser.addValueEventListener(new ValueEventListener() {....}

#2


1  

FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
DatabaseReference mFirebaseDbReferenceUserChild = FirebaseDatabase.getInstance().getReference("users");
DatabaseReference mFirebaseDbReferenceCurrentUser= mFirebaseDatabaseReferenceUserChild.child(mFirebaseUser.getUid());
mFirebaseDbReferenceCurrentUser.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            // TO GET USERDETAILS FOR CURRENT USER
            String userDetails = dataSnapshot.getValue().toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

This way you can fetch data of CURRENT USER from "users" table by passing CURRENT USER's uId.

这样,您可以通过传递CURRENT USER的uId从“users”表中获取CURRENT USER的数据。