push javascript == Undefined

Asked

Viewed 59 times

-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 gives typeof this.objectArrey, Array.isArray(this.objectArrey)); in the first line of that function (before the forEach)

  • I didn’t understand very well, but I did it and kept making a mistake.

  • It showed something on the console?

  • the application or compile.

  • 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

  • ok, so there was an error in Typescript... this error should be visible in the compiler

  • there is no Pow huahuau if I had not read the Pow error

Show 2 more comments

1 answer

-2

To solve, just change the objectArrey variable to the following form:

objectArrey: Object[] = []

Browser other questions tagged

You are not signed in. Login or sign up in order to post.