Firestoreb collection foreach loop returns empty id

Viewed 28

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 ?

0 Answers
Related