简体   繁体   中英

Cannot read property “push” of undefined

I have an error when I try to push a value in an array. The mystic thing in this error is that it only occurs on the Ionic Dev App, whereas in the Google Chrome Console, everything works perfectly.

I did initialize the array, so the error doesn't come from here. Maybe, the error comes from an asynchronous operation, but I don't see where I don't catch a promise...

initModelesList(){
  this.addModele = this.navParams.get('addModele') || false;
  this.deleteModele = this.navParams.get("deleteModele") || false;
  if (!!this.addModele){
    this.modelesList.push(this.addModele); //error here
    this.saveModelesList();
  }
  if (!!this.deleteModele){
    const index: number = this.modelesList.indexOf(this.deleteModele);
    if (index !== -1){
      this.modelesList.splice(index,1)
    }
    this.saveModelesList()
  }
}
saveModelesList(){
 this.storage.set('modelesList',this.modelesList)
}
getModelesList(){
  this.storage.get('modelesList').then((modelesList) => {
    this.modelesList=modelesList;
    this.initModelesList()
  },()=>{
    this.modelesList=[];
    this.initModelesList();
  })
}

Here's a screenshot of the error on the Ionic Dev App.

在此处输入图片说明

Make sure you have initialized modelesList

You can explicitly check if it's not null and create one if needed:

this.modelesList= this.modelesList|| [];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM