Skip to content Skip to sidebar Skip to footer

Cannot Read Property 'propostas_realizadas' Of Undefined - Angular 2

I have an application in Angular 2 in which I'm trying to print some data on my console 'console.log(this.disputa.propostas_realizadas)', however when I try to print it I get this

Solution 1:

You are console logging the response before it has been assigned, this is an async operation.

ngOnInit() : void{  
  this.route.params.subscribe(params =>{
  let id = params['id'];
  this.service
    .buscaPorId(id)
    .subscribe(disputa => {
      // executed sometimes later...this.disputa = disputa;
      console.log(disputa);
    },
    erro =>console.log(erro));
  })
   // executed firstconsole.log(this.disputa.propostas_realizadas);
}

Notice the comments in above code. To have your data available, you need to do it inside the subscription, so the following should work:

ngOnInit() : void{  
  this.route.params.subscribe(params =>{
  let id = params['id'];
  this.service
    .buscaPorId(id)
    .subscribe(disputa => {
      this.disputa = disputa;
      console.log(disputa);
      // here!console.log(this.disputa.propostas_realizadas);
    },
    erro =>console.log(erro));
  })
}

Post a Comment for "Cannot Read Property 'propostas_realizadas' Of Undefined - Angular 2"