'How to read firestore data in react native?

Hello I am new to react native and particullary firebase. I've been watching tutorial about using firebase with react native and i read the react native firebase documentation but i am stuck. I have data in my firestore data base, in my collection called user but I am not able to read, to get the data from it. Here is my firestore database : Here is my firestore database

and here is how I tried to get the data in my component :

const Profile = ({navigation, ...props}) => {



    async function getUserData () {
        const userCollection = await await firebase.firestore().collection('users').doc(firebase.auth().currentUser.uid).get()
        return userCollection
    }
    console.log('🐲' + getUserData())

this return me that : [object Object]

I also tried this, (it was how it was done in the instagram clone tutorial of Adrian Twarog) :

const Profile = ({navigation, ...props}) => {

  function getUserDataFirstTry() {
        firebase.firestore()
        .collection("users")
        .doc(firebase.auth().currentUser.uid)
        .get()
        .then((snapchot) => {
            if(snapchot.exists){
                console.log('🦜' +  snapchot.data())
            } else {
                console.log('merde ca marche pas')
            }
        })
    }

    console.log('🐲🐲' + getUserDataFirstTry())

But I get the same result in my console : [object Object]

I tried to put my function in a useEffect hook but this changes nothing. Can you tell me what I am doing wrong ? (if possible in the second example because it's realtime changes). Thanks.



Solution 1:[1]

As Nicholas commented, since you're concatenating snapchot.data() to a string, you get the string representation of that object, which is pretty useless.

To log a specific field from the data:

console.log('?' +  snapchot.data().userName)

To log all fields from the data in a more useful format:

console.log('?' +  JSON.stringify(snapchot.data()))

Note: the proper spelling is snapshot (not snapchot), but I kept your original spelling here for ease of copy/paste.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen