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

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
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