crud.js文件报错Cannot read properties of undefined (reading ‘vm‘)

报错

说明 findVM 方法里,this.vms[i] 是 undefined,导致 this.vms[i].vm 报错。

1. 问题根源分析

crud.js 相关代码大致如下

findVM(type) {
  for (let i = 0; i < this.vms.length; i++) {
    if (this.vms[i].type === type) {
      return this.vms[i].vm;
    }
  }
  return undefined;
}

如果 this.vms[i] 是 undefinedthis.vms[i].type 就会报错。

2. 如何修复?

只需要在 findVM 方法里加一层健壮性判断:

findVM(type) {
  for (let i = 0; i < this.vms.length; i++) {
    if (this.vms[i] && this.vms[i].type === type) {
      return this.vms[i].vm;
    }
  }
  return undefined;
}

 这样就不会再报 Cannot read properties of undefined (reading 'vm') 了。

3. 建议你这样改

请在 src/components/Crud/crud.js 的 findVM 方法里加上 this.vms[i] && 的判断。即可

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝胖子的多啦A梦

你的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值