vue 学习笔记(三)报错: TypeError: Cannot set property '0' of undefined


 TypeError: Cannot set property '0' of undefined

具体报错代码如下


submit () {
back. submitForm(). then(( response) => {
let sets = []
if ( response. data !== undefined) {
for ( let i = 0; i < response. data. length; i++) {
console. log( response. data[ 0]. name)
let groupinfo = {}
groupinfo. name = response. data[ i]. name
for ( let j = 0; j < response. data[ i]. total; j++) {
let ingroup = {}
ingroup = response. data[ i]. powerSockets[ j]
groupinfo. items[ j] = ingroup
}
sets[ i] = groupinfo
}
}
this. list = sets
})
}


groupinfo.items[j] = ingroup

该行报错,只找到空值。

解决方法: 

submit () {
back. submitForm(). then(( response) => {
let sets = []
if ( response. data !== undefined) {
for ( let i = 0; i < response. data. length; i++) {
console. log( response. data)
let groupinfo = {}
let ingroup = []
if ( response. data[ i]. powerSockets !== undefined) {
for ( let j = 0; j < response. data[ i]. total; j++) {
let obj = {}
obj. item = response. data[ i]. powerSockets[ j]. used
ingroup[ j] = obj
}
}
groupinfo. name = response. data[ i]. name
groupinfo. children = ingroup
sets[ i] = groupinfo
}
}
this. list = sets
})
}
总结:小心谨慎,注意每个值的赋值情况
 TypeError: Cannot set property '0' of undefined

你可能感兴趣的:(vue学习)