我正在使用Vue.JS和Laravel 5.2进行项目。VueJS - 有时会出错“Uncaught TypeError:无法读取未定义的属性'进程'
这是一个训练词汇的程序。所以在我的Vue-Data-Object中有一个包含wordpairs的数组。每个wordpair被存储作为对象:
"words": [
{
"lang1": "Haus",
"lang2": "house",
"lang1_hint": "",
"lang2_hint": "",
"image_url": "",
"tries": 0,
"fails": 0,
"process": 1
},
{
"lang1": "Feuer",
"lang2": "fire",
"lang1_hint": "",
"lang2_hint": "",
"image_url": "",
"tries": 0,
"fails": 0,
"process": 5
},
...
]
现在我有一个函数,该函数数组的长度,产生一个随机数,并返回一个随机数出数组的上面:
getRandomWord: function(){
var i = Math.floor((Math.random() * this.words.length) + 1);
if(this.words[i].process == 5){
return this.getRandomWord();
} else {
return {
index: i,
content: this.words[i]
}
}
}
大部分没有问题。但有时,出现错误时的说法:
Uncaught TypeError: Cannot read property 'process' of undefined
控制台说,在我的getRandomWord()if(this.words[i].process == 5)
的,如果条件发生错误。
任何想法为什么?过程值始终为1,2,3,4或5.
感谢至此!
天哪...谢谢!我知道一个数组从0开始,但这不是我寻找解决方案的方向。 – Brotzka