VueJS-有时会收到错误“未捕获的TypeError:无法读取未定义的属性'process'”

布罗茨卡

我正在使用Vue.JS和Laravel 5.2进行项目。

这是一个训练词汇的程序。所以我的Vue-Data-Object中有一个数组,其中包含单词对。每个单词对都存储为一个对象:

"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条件中if(this.words[i].process == 5)

知道为什么吗?进程的值始终为1、2、3、4或5。

到目前为止,谢谢!

理查德·H

Javascript数组的第一个索引为0,因此数组的最后一个索引为array.length - 1您用于生成索引的代码:

  Math.floor((Math.random() * this.words.length) + 1);

有时会生成-的索引,该索引length不存在。删除+1,您应该会很好。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

VueJS和Firestore-未捕获(承诺)TypeError:无法读取未定义的属性

VueJS =未捕获的TypeError:无法读取未定义的属性“设置”

VueJS 3 + Laravel:未捕获的TypeError:无法读取未定义的属性“ component”

VueJS:未捕获(承诺)TypeError:无法读取未定义的属性“ rol”

渲染中的Vuejs错误:“ TypeError:无法读取未定义的属性'props'” Vuejs

VueJS TypeError:无法读取未定义的属性“ className”

TypeError:无法读取未定义Vuejs的属性'$ auth'

VueJs [Vue警告]:渲染错误:“ TypeError:无法读取未定义的属性'title'”

Laravel Vuejs 错误:未捕获的 ReferenceError:未定义登录

VueJS-无法读取未定义的属性“状态”

VueJS:无法读取未定义的属性“ dispatch”

VueJS - 为什么 Vue 组件的 HTML 部分未定义 process.env 文件?

TypeError:无法设置未定义的属性'posts'-Vuejs

VueJS 获取 TypeError:删除并添加传感器信息时无法读取未定义的属性

TypeError:无法读取VueJS上下文中未定义的属性“替换”

VueJs计算的方法错误:无法读取未定义的属性“ words”?

Node.js child_process TypeError:无法读取未定义的属性“_writableState”

VueJS TypeError 无法读取的属性

Vuejs @click事件有时会出错

我为什么会收到此错误:Uncaught TypeError:无法读取未定义的属性“ john”

操作SDK会收到此错误TypeError:无法读取未定义的属性“输出”

无法在Chrome扩展程序中创建通知。收到未捕获的TypeError:无法读取未定义错误的属性“创建”

如何修复VueJS中的“属性或方法未定义”错误

Vuejs 属性或方法“welcome”未定义

VueJS mixins未定义属性

为什么我会收到未捕获的类型错误:如果声明了 var,则无法读取未定义的属性“长度”?

在OSX上的Mono,Process.Start有时会引发无法处理的本机包装异常

Vuejs有效载荷在突变中未定义。“无法设置未定义的属性”

VueJS路由未定义