简体   繁体   中英

How to get current user data from firebase realtime database in React Native

I'm trying to get current signed user's image and name from the firebase realtime database in which his data was stored when he signup. I want to put these two items in the drawer header. enter image description here You can see the attached screenshots.

Here is the current user在此处输入图片说明

here is the data of the current user. I want to get that data on the basis of current user signed in

在此处输入图片说明

you can get the currentUser.uid if you are loggedin with firebase.

try my code,

firebase
      .database()
      .ref('Users/' + firebase.auth().currentUser.uid)
      .once('value')
      .then(snapshot => {
        console.log(snapshot.val()) <--will print the user data
      });

hope this works for you.

Try making an async function so you can retrieve data from any user with just the UID like this:

export async function getUserByUID(UID) {
  return await firebase.database().ref(`Users/${UID}`).once('value');
}

Later you can simply pass the UID and get any users' data:

const getMyData = async () => {
  let UID = firebase.auth().currentUser.uid;
  let userData = await getUserDataByUID(UID);
  // this will be null if there's no user data
  // all users' data will be in userData for example:
  console.log(userData.image) // will log the image url you saved.
}

BTW don't forget your firebase database rules! The last thing you want is other seeing those passwords!!

check the following code

 firebase.database().ref(`Users/${firebase.auth().currentUser.uid}`).once('value', function (snapshot) {
        console.log(snapshot.val())
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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