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

153
Views
Array is null when calling `.push`

I am working on an angular app. In my component .ts file I have an array as follow:

public myArray = [];
public dataFromAPI  = [];

In one of my method whenever I am trying to push a element in this array, it is giving me following error:

   this.appService.getData.subscribe(resp => {
      if (resp != null) {
          this.dataFromAPI = resp;
          this.dataFromAPI.forEach(element => {
            this.myArray.push(element)
          })
      }

RROR TypeError: Cannot read properties of null (reading 'push')

I don't see any mistake in my code. I always define a new array like this but first time I am getting this error. How to resolve this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

the declaration should be like below

public myArray:any = [];
public dataFromAPI:any  = [];

you should directly loop the response of the api like below (please check inside there are may be more keys inside response object, access the array through key)

   this.appService.getData.subscribe(resp => {
      if (resp != null) {
          resp.data.forEach(element => {
            this.myArray.push(element)
          });
      this.dataFromAPI = resp;
      }
   }
about 4 years ago · Juan Pablo Isaza Report

0

I'm not sure what syntax public myArray = []; is. Below will work for typescript.

const myArray: string[] = [];
myArray.push('string');

const myArray: number[] = [];
myArray.push(123);

const myArray: Record<string, unknown>[] = [];
myArray.push({key: 'value'});
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!