Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda