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

198
Views
Pulling a variable out of Promise when querying data from Firestore Firebase database - Javascript
let savedArrayUID = []; let savedArrayEmails = [];

function pullIt(emailSearch) {

    db.collection(collectionName).where('email', '==', emailSearch).get()
        .then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                savedArrayUID.push(doc.id);
                savedArrayEmails.push(doc.data());
                // doc.data() is never undefined for query doc snapshots

                console.log(doc.id, " => ", doc.data());
                // saved.   push(doc.id);
                return savedArrayUID; 
            })
        });
}

I can query the data from the database but cannot pull the variable out of the scope of the function. I want to use this function to pass through emails to find info of their profile saved in my Database.

I really struggle to understand how Promiseses can help here. I have a feeling this is already solved, but I could not find an answer anywhere.

about 4 years ago ยท Santiago Trujillo
1 answers
Answer question

0

There's two steps to this:

  1. Ensure that your data makes it out of your pullIt (as a promise).
  2. Then call that pullIt correctly, waiting for the promise to resolve.

In code, you're missing a top-level return the pullIt code:

function pullIt(emailSearch) {
    // ๐Ÿ‘‡
    return db.collection(collectionName).where('email', '==', emailSearch).get()
        .then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                savedArrayUID.push(doc.id);
                savedArrayEmails.push(doc.data());
            })
            return savedArrayUID; // ๐Ÿ‘ˆ
        });
}

And then when calling pullIt, you'll need to use either await or then, to ensure pullIt completed before you try to access the result.

So either:

pullIt("yourSearchTeam").then((results) => {
  // TODO: use your results here
})

Or (in an async context):

const results = await pullIt("yourSearchTeam")

// TODO: use your results here
about 4 years ago ยท Santiago Trujillo 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!