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
Need to get an array of all Id's in a firebase collection

Have the method below that extracts the Id's from the firebase collection and pushes it to an array, BUT it makes each document into an array. In other words- if i have 100 documents in the collection - it makes 100 arrays inside the array IDarray. I just need one array to keep it more efficient. Maybe:)

     db.collection('Customers')
     .orderBy('createdAt', 'asc')
     .get()
     .then((data) => {
                      let IDarray = [];
                      data.docs.forEach(doc =>{
                      IDarray.push(+doc.id);
                      });
       return [IDarray];
     })

I can certainly make this work but if anyone knows of a better way so the system is not generating all the extra arrays it would be appreciated. (Back story: the IDarray is being generated as int because my Id's for the collection are a number sequence) Cheers!

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

0

Try this:

  db
    .collection("Customers")
    .orderBy("createdAt", "asc")
    .get()
    .then((data) => {
      let IDarray = [];
      data.docs.forEach((doc) => {
        IDarray.push(+doc.id);
      });
      return IDarray;
    });

It works just fine for me.

Or here's an ES6-y way of doing it:

  db
    .collection("Customers")
    .orderBy("createdAt", "asc")
    .get()
    .then(({ docs }) => docs.map(({ id }) => +id));
about 4 years ago · Juan Pablo Isaza Report

0

Seems like you don't need another array wrapper for the return value:

    return IDarray;

Notice no square braces. Just return the array as-is.

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!