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

108
Views
Firestoreb collection foreach loop returns empty id

in my collection (Campaign), I want to return a specific document id by a campaignName

this is my method :

getCampaignId(): string {
    const campaignCollection = this.afs
      .collection<Campaign>('mail_campaign')
      .snapshotChanges();

    campaignCollection.forEach((value) => {
      return value.map((res) => {
        if (res.payload.doc.data().campaignName === 'x') {
          this.campaignId = res.payload.doc.id;
        }
        return '';
      });
    });
    return this.campaignId;
  } 

inside the if , I get the id if I print it directly in the console :

console.log(res.payload.doc.id);

but if I print my global variable this.campaignId in the constructor, I get an empty string (the method is called in the constructor before printing the variable in the console)

I want to use this variable outside of this class, but I can't because it's returning an empty string.

Any ideas ?

about 4 years ago · Santiago Trujillo
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!