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

388
Visualizações
Angular: ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'forEach')

I try execute on my cards.component.ts

    notices: notice[];

    ngOnInit() {
      this.dataService.getCards().subscribe( data => { this.notices = data });
      this.notices.forEach(n => {
        console.log('test')
      });
    }

I only want show something in console for every item is on "notices" but show me this error

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'forEach')

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Array notices should be initialized in field declaration or in constructor of component

notices: notice[] = [];

or

constructor() { this.notices = [] }

about 4 years ago · Juan Pablo Isaza Relatório

0

Most likely your subscribe is getting called after your call to forEach The best fix would be to initialize your notices to an empty array and then loop through this.notices inside the subscribe call:

notices: notice[] = [];

ngOnInit() {
  this.dataService.getCards().subscribe( data => { 
  this.notices = data 
  this.notices.forEach(n => {
     console.log('test')
  });
 });     
}
about 4 years ago · Juan Pablo Isaza Relatório
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