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;
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);
});