0
const set = firebase.firestore().collection("workoutExercises").doc(firebase.auth().currentUser.uid).get()
  console.log(set)

Terminal:

Promise {
  "_U": 0,
  "_V": 0,
  "_W": null,
  "_X
}

Firestore document:

enter image description here

Hello everyone, I am trying to get data from Cloud Firestore, when I run the code it only returns the code in the Terminal above, I am using React Native, and the goal is to store the Array set from Firebase to the const set in the screen. Thank You

Salvo
  • 107
  • 9
  • Does this answer your question? [How to save a result from a Firestore query into a javascript variable?](https://stackoverflow.com/questions/67906378/how-to-save-a-result-from-a-firestore-query-into-a-javascript-variable) – Matt U Jun 15 '21 at 22:25
  • How are you instantiating firebase and firestore? – Joel Hager Jun 15 '21 at 22:27
  • @MattU no really – Salvo Jun 15 '21 at 22:47

1 Answers1

1

Firebase calls like this are asynchronous, meaning they don't return immediately. In Javascript, you can deal with this in a couple of different ways, using async/await or Promises.

Here's an example using a Promise:

firebase.firestore().collection("workoutExercises").doc(firebase.auth().currentUser.uid).get()
.then(document => {
  console.log(document.data());
})
.catch(error => console.error(error));

The question/answer linked in the comments shows you how to do this with async/await.

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • thank you, it works, but it returns in the terminal a bunch of data I don't need, I only need the values in the Firebase document so I can use them in a Flatlist. – Salvo Jun 15 '21 at 23:55
  • Updated my answer to reflect this. Also, see documentation at: https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document – jnpdx Jun 15 '21 at 23:56
  • 1
    I've struggling with this for days, thank you very much – Salvo Jun 15 '21 at 23:59