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

157
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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