1、 微信小程序报这类错误:
(in promise) MiniProgramError Cannot read property ‘findIndex’ of null TypeError: Cannot read property ‘findIndex’ of null
data: {
addval:"",
editval:"",
typeList:[]
},
// 添加
async add(){
let typeName = this.data.addval
// 判断分类名称是否重复
// console.log(this.data.typeList);
const index = this.data.typeList.findIndex(item => {
return item.typeName == typeName
})
问题所在:
发现当typeList中的值为空时,就会报这个错误,有 数据时就不会报错
解决办法:
在循环之前加一个判断
**this.data.typeList= this.data.typeList || [ ]**
// 判断分类名称是否重复
var index = this.data.typeList.findIndex(item => {
return item.typeName == typeName
})