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

389
Views
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 answers
Answer question

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 Report

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 Report
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!