7

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.

Hardik Virani
  • 1,687
  • 7
  • 23
  • 36
Saumay Paul
  • 405
  • 1
  • 7
  • 21

2 Answers2

9

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.

Saumay Paul
  • 405
  • 1
  • 7
  • 21
0

For dummies in firebase like me, if you want a custom document id, you have to specify it when writing data to firestore

import firestore from '@react-native-firebase/firestore';

firestore()
  .collection('Users')
  .doc('ABC')
  .set({
    name: 'Ada Lovelace',
    age: 30,
  })
  .then(() => {
    console.log('User added!');
  });

Then you can get it by

import firestore from '@react-native-firebase/firestore';

const user = await firestore().collection('Users').doc('ABC').get();

For reference: https://rnfirebase.io/firestore/usage#writing-data

Muaz
  • 133
  • 2
  • 12