Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
Firestore data returns undefined after reading it from database in javascript

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;

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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);
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!