Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to fetch data from Firebase Firestore in react native

I am trying to GET data from Firestore but I cannot see data coming. I am building a react-native app. I am using react-native-firebase.

I also tried Transactions method from react-native-firebase documentation. But nothing is working.

Here' my code:

firebase.firestore().collection('users').doc(uid)
          .get()
          .then((doc)=>{ 
                  console.log(doc)
                  console.log(doc.data().shoppinglist)   
          })
          .catch(e => console.log(e));

Console logging .get() gives me a Promise.

I am getting the Promise like this:

Promise {_40: 0, _65: 0, _55: null, _72: null}
_40: 0
_55: null
_65: 0
_72: null

But .then() doesn't executes as the two console.log() ain't logging anything.

Please help me out here. Quite new with Firebase.

like image 431
Saumay Paul Avatar asked Apr 02 '19 13:04

Saumay Paul


1 Answers

After some digging in Firebase Documentation, I found out a solution.

Collection references and document references are two distinct types of references and let you perform different operations. For example, you could use a collection reference for querying the documents in the collection, and you could use a document reference to read or write an individual document.

Therefore, Replacing firebase.firestore().collection('users').doc(uid) with firebase.firestore().doc(`users/${uid}`) solved my problem.

like image 62
Saumay Paul Avatar answered Sep 28 '22 07:09

Saumay Paul