Tengo un vector de un tipo de dato (clase) definida por mi. El error que tengo es que al intentar imprimir desde la posicion 1 en adelante me muestra esto: "_this.inmuebles[1] is undefined" , si intento imprimir solo la posicion 0 si me lo muestra bien. Imprimi la longitud del vector inmuebles y al parecer si se esta insertando la variable "inmueble" porque muestra la longitud correcta, pero no se que pasa. Dejo el código a continuacion:
import { Component, OnInit } from '@angular/core';
import { HostlistService } from '../servicios/hostlist.service';
import {$,jQuery} from 'jquery';
import { Inmueble } from '../modelos/inmueble';
@Component({
selector: 'app-slider',
templateUrl: './slider.component.html',
styleUrls: ['./slider.component.css']
})
export class SliderComponent implements OnInit {
inmuebles: Inmueble[] = [];
i = 1;
url: string = "d782a4ae-733f-b7c4-ed11-5ba553455e04_fot01_c";
constructor(private hostlistService: HostlistService) { }
ngOnInit() {
this.cargarJson();
}
anteriorInmueble(){
this.i=this.i-1;
}
siguienteInmueble(){
this.i=this.i+1;
}
// metodo para cargar el Json recibido de la petición
cargarJson(){
var foto1,foto2: string;
foto1 = "fot0", foto2 = "fot";
var pos: number;
this.hostlistService.getInmuebles().subscribe(
result => {
if(result.success === true){
for(const item of result.data) {
pos = 0;
const inmueble = new Inmueble();
inmueble.nombre = item.nomb_prod_c;
inmueble.id = item.id;
inmueble.estacionamiento = item.estan_c;
inmueble.baño = item.banof_c;
console.log(inmueble.nombre);
(item.fot01_c != "") ? inmueble.fotos[0]="true": inmueble.fotos[0]="false"; pos++;
this.inmuebles.push(inmueble);
console.log("----->"+this.inmuebles[0].fotos[0]);
console.log("----->"+this.inmuebles[1].fotos[0]);
}
}
},
error => {
console.log(error);
}
);
}
}
esta es la clase Inmueble:
export class Inmueble {
nombre: string;
id: string;
baño: number;
estacionamiento: number;
metro: number;
precio: number;
fotos: boolean[];
}
Espero puedan ayduarme, tengo horas intentando resolver y no doy con el error.