为什么报错?我的代码是这样的const loadingPort= this.portlist.filter((item)=>{ return item.portName==this.form.loadingPort }) if (loadingPort.length==0){ this.$modal.msgError("请重新选择起运港"); }, 报错信息是Cannot read properties of undefined (reading 'filter')"
时间: 2024-03-21 19:39:15 浏览: 100
这个报错信息很明显是说你的 `this.portlist` 是 `undefined`,导致无法调用 `filter()` 方法。你可以在使用 `filter()` 方法前,先判断一下 `this.portlist` 是否存在或者是否有值。可以像这样:
```
if (this.portlist && this.portlist.length > 0) {
const loadingPort = this.portlist.filter((item) => {
return item.portName == this.form.loadingPort;
});
if (loadingPort.length == 0) {
this.$modal.msgError("请重新选择起运港");
}
} else {
this.$modal.msgError("起运港列表为空");
}
```
这样就可以避免 `this.portlist` 为 `undefined` 导致报错的情况了。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)