-2
I’m trying to send an object inside a arrey to get a sequence of objects but when I use the push it error function saying that Undefined I’ll leave the codes below:
objectArrey: Object[]
//esse é a minha variável onde todos os objetos tem que estar
//nesse código é onde eu faço um forEach no meu arrey que chegou da api a adiciono extraio desse objeto as informações necessárias e na hora do push da o erro Cannot read property 'push' of undefined.
this.api.getAll().subscribe(
      data => {
        let arreyAst = data.near_earth_objects['2015-09-08'];
        let ast = {}
        arreyAst.forEach(element => {
          ast = {
              name: element.name,
            }
          this.objectArrey.push(ast)
        });
        console.log(this.objectArrey)
      },
      error => {
        console.log('Deu erro ' + error)
      })
Full code of ts:
import { Component, OnInit } from '@angular/core';
import { ApiServiceService } from '../api-service.service';
@Component({
  selector: 'app-object',
  templateUrl: './object.component.html',
  styleUrls: ['./object.component.scss']
})
export class ObjectComponent implements OnInit {
  objectArrey: Object[]
  constructor(private api: ApiServiceService) { }
  ngOnInit(): void {
    this.getObj()
  }
  getObj(){
    this.api.getAll().subscribe(
      data => {
        let arreyAst = data.near_earth_objects['2015-09-08'];
        let ast = []
        arreyAst.forEach(element => {
          ast = [
            {
            name: element.name,
            }
          ]
          this.objectArrey.push(ast)
        });
        console.log(this.objectArrey)
      },
      error => {
        console.log('Deu erro ' + error)
      })
  }
}
Where defines
this.objectArrey? What givestypeof this.objectArrey, Array.isArray(this.objectArrey));in the first line of that function (before theforEach)– Sergio
I didn’t understand very well, but I did it and kept making a mistake.
– lima23
It showed something on the console?
– Sergio
the application or compile.
– lima23
Guy managed to solve, it was a very silly mistake that ends up passing unnoticed, thanks for the help the solution is objectArrey: Object[] = [] is just declare an empty arrey
– lima23
ok, so there was an error in Typescript... this error should be visible in the compiler
– Sergio
there is no Pow huahuau if I had not read the Pow error
– lima23