Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda