0

I'm just starting with firebase-firestore and I want to use the collected data later in my function but when I try call this data, returns empty or undefined.


//This comes from a previous firestore call

doc.ref.collection("guardias").doc(diasem).get().then((doc) => {
if(doc.exists) {

var obj = doc.data();
var guardiasn = Object.keys(obj).map((key) => [String(key), obj[key]]);

    } else {
        //doc.data() will be undefined in this case
        alert("Se ha producido un error en la base de datos. Por favor, ponte en contacto con el administrador")
    }
  }).catch((error) => {
    console.log("Se ha producido un error en la base de datos. Por favor, ponte en contacto con el administrador:", error);
  });

console.log(guardiasn) 

returns undefined;

Osvaldo
  • 473
  • 1
  • 12

1 Answers1

0

var guardiasn = Object.keys(obj).map((key) => [String(key), obj[key]])

Can I first of all ask why you need to include the above code?

I'd probably structure the code something like this:

var guardias = db.collection("guardias").doc(diasem);

// I'm not sure on your data types here, 
// but you could either push the data to an array 
// or update a variable like so:

const data = [];

guardias.get().then((doc) => {
    if (doc.exists) {
        data.push(doc.data());
        console.log("Document data:", obj);
    } else {
        // doc.data() will be undefined in this case
        alert("Se ha producido un error en la base de datos. Por favor, ponte en contacto con el administrador")
    }
}).catch((error) => {
    console.log("Se ha producido un error en la base de datos. Por favor, ponte en contacto con el administrador:", error);
});
  • Let me know if this helps! – Owen Halliday Dec 16 '21 at 18:27
  • 1
    ```var guardias = await db.collection("guardias").doc(diasem);``` Firestore calls are asynchronous – LeadDreamer Dec 16 '21 at 19:35
  • Thanks but the point is how can I store the data of "guardiasn" outside the firebase iteration. This is only for convert the object in a simple array: ```var guardiasn = Object.keys(obj).map((key) => [String(key), obj[key]])``` – Mario Moreno González Dec 17 '21 at 12:23
  • @LeadDreamer https://firebase.google.com/docs/firestore/query-data/get-data#web-version-8_1 There's no need for the `async` attribute here as we're using `.then()` in order to handle the `async` request. There's a bunch of reasons why we should probably just use `await` here, https://stackoverflow.com/questions/54495711/async-await-vs-then-which-is-the-best-for-performance but I like to follow the documentation when answering these questions so it's easier for the OP to follow. – Owen Halliday Dec 17 '21 at 18:48
  • I've updated my answer – Owen Halliday Dec 17 '21 at 18:54
  • Apparently, the data is not being correctly retrieved from Firestore, it seems something is missing. What documentation are you using to create the code? Please share any documentation or tutorial you are following, as well as the exact error your console throws when you run the code. Here is a very useful [video](https://www.youtube.com/watch?v=kmTECF0JZyQ&t=736s) about getting data from Firebase. – Andres Fiesco Casasola Dec 17 '21 at 20:21
  • Thanks a lot everyone. I've created an array and pushed the element to it. But in the console I can see the array, but appears empty and with length=0. But if I unfold the array in the console I can see the array with the data in the 0 position so the array should be length=1. Is the array bad constructed? ```var datahoras = []; var clouddb = firebase.firestore(); var centro = clouddb.collection("centros").doc(centrodef); //ENCABEZADOS, FALTAS Y GUARDIAS centro.get().then((doc) => { if (doc.exists) { var encabs = doc.data().mapahoras; datahoras.push(encab);``` – Mario Moreno González Dec 21 '21 at 13:28