Фильтр панели поиска ионных V3 с JSON - PullRequest
0 голосов
/ 24 апреля 2019

Я пытаюсь создать панель поиска в ionic v3 с данными JSON. JSON работает отлично. Но когда я пытаюсь получить некоторые значения, это становится неопределенным (дать обещание). Эта строка "console.log (this.empresas [0] .empresaNombre);" является проблемой.

searchQuery: string = '';
items: string[];

empresas: any = [];

constructor(...) {
this.initializeItems();
}

initializeItems() {
this.getEmpresas2().then(data => {
this.empresas = data;
console.log(this.empresas[0].empresaNombre); 
// Here it says that is undefined
});
}

getItems(ev: any) {
// Reset items back to all of the items
this.initializeItems();

// set val to the value of the searchbar
const val = ev.target.value;

// if the value is an empty string don't filter the items
if (val && val.trim() != '') {
this.items = this.items.filter((item) => {
return (item.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}

getEmpresas2() {
return new Promise(resolve => {
this.http.get("http://getjson").subscribe(
data => {
resolve(JSON.parse(data["_body"]));
},
error => {
}
);
});
}
}
...