TypeError: Cannot read properties of null (reading ‘map‘)
let result = JSON.parse(result.data)
this.bases = result.map(base => ({
id: base.guid,
name: base.SiteName
}))
result是从后端请求到的xx数据,当请求到的数据不为null时,页面正常显示。但是当请求到的result为null时,此时会出现报错:TypeError: Cannot read properties of null (reading ‘map‘)。
解决方法:
let result2 = JSON.parse(result.data) || []
this.bases = result2.map(base=> ({
id: base.guid,
name: base.SiteName
}))
在 let result2 = JSON.parse(result.data) 后面加上 || [] 即可。
上一篇: 几秒钟就充满电!科学
下一篇: 暂无数据