I have a problem with angular 8 (used in ionic 5). My class has the method initPizzas(). Here the injected pizzaModel is used. initPizzas() is called in ngOnInit() and everything works fine. But when I call delete(), initPizzas() is triggered in the then() function of the prmise returned by the deleteBy() function and I get the error:
Uncaught Error: Uncaught (in promise): TypeError: Cannot read property 'pizzaModel' of undefined TypeError: Cannot read property 'pizzaModel' of undefined
import {Component, OnInit} from '@angular/core';
import {PizzaModel} from './pizza-model';
import {CoreService} from '../core/core.service';
import {IonItemSliding} from '@ionic/angular';
@Component({
selector: 'app-pizzas',
templateUrl: './pizzas.page.html',
styleUrls: ['./pizzas.page.scss'],
})
export class PizzasPage implements OnInit {
pizzas: any[];
constructor(private pizzaModel: PizzaModel, private coreService: CoreService) {
}
ngOnInit() {
this.initPizzas();
}
initPizzas() {
// after called in ngOnInit() everything's fine
// after delete() is called and the promise had been resolved the following "this" is "undefined" :-(
this.pizzaModel.findAll({
order: 'date DESC'
}).then((pizzas) => {
this.pizzas = pizzas;
});
}
delete(item: IonItemSliding, pizza: PizzaModel) {
this.pizzaModel.deleteBy().then(this.initPizzas).catch(this.initPizzas);
}
}
// pizzaModel (simplified for the sake of simplicity)
deleteBy(query: string, values): Promise<any> {
return new Promise((resolve, reject) => {
resolve();
});
}
Any help is highly appreciated. I really don't get it :-(
Thanks in advance!