Cannot read property 'forEach' of undefined

在调bug的时候出现了这个问题,这个问题可能会导致界面出现重复显示的问题

错误代码:

//加载表格内容
      if (res != null && res.code == '0000' && res.data != null) {
        let ret =[];
        
        res.data.list.forEach(el => {
          const item: any = {};
          item.studentCode = el.studentCode;
          item.studentName = el.studentName;
          item.studentScore = el.studentScore;
       }

这个就是遍历数据的一个方法。

解决办法就是在遍历之前,先判断是否存在

if (!res.data.list){
          return;
        }

然后就解决了。

你可能感兴趣的:(Cannot read property 'forEach' of undefined)