Firebase: How to log the value returned from Firebase collection?

In my React Native app I get a value from Firebase using

this.getRef()
.doc(<something>)
.collection(<something>)
.doc(<something>)

I want to log the value returned by that, but I don't know if it returns a promise. I'd like to do something like

let a = this.getRef()
  .doc(<something>)
  .collection(<something>)
  .doc(<something>)
console.log(a)

How should I approach this?


Solution 1:

At the moment, you just have a reference to the document, not the document itself. To get the document a single time, use .get. This will return a promise:

this.getRef()
  .doc(<something>)
  .collection(<something>)
  .doc(<something>)
  .get()
  .then(doc => {
    console.log(doc.data());
  })

// Or using async await:
const someFunction = async () => {
  const doc = await this.getRef()
    .doc(<something>)
    .collection(<something>)
    .doc(<something>)
    .get()
  console.log(doc.data());
}

Alternatively, if you want to continue listening for changes, use onSnapshot:

const unsubscribe = this.getRef()
  .doc(<something>)
  .collection(<something>)
  .doc(<something>)
  .onSnapshot(snapshot => {
    console.log(snapshot.data());
  });

// Later you can call unsubscribe() to stop listening