无法读取属性"push"未定义 [英] Cannot read property "push" of undefined

查看:82
本文介绍了无法读取属性"push"未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将值推入数组时出现错误.该错误的神秘之处在于它仅在Ionic Dev App上发生,而在Google Chrome控制台中,一切运行正常.

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();
  })
}

这是Ionic Dev App上错误的屏幕截图.

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

推荐答案

确保已初始化 modelesList

Make sure you have initialized modelesList

您可以显式检查它是否不为null并根据需要创建一个:

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

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

这篇关于无法读取属性"push"未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆