// 获取分类
async getCategory () {
const categorys = await Category.findAll({
where: {
delete_time: null
}
});
let father = categorys.filter( item => item.pid === 0);
let son = categorys.filter( item => item.pid !== 0);
let data = [];
// forEach 不支持 async await
for(let item = 0; item < father.length; item++){
let children, currentItem;
for(let i = 0; i< son.length; i++){
if(son[i].pid === father[item].id){
currentItem = son[i];
break;
}
}
const attr = await Attr.findAll({
where: {
cate_id: currentItem.id,
}
})
children = {
...currentItem,
attr
}
let obj = {
...item.dataValues,
children
}
data.push(obj)
}
return data;
}
思考: ...currentItem 他是一个浅拷贝,会造成循环引用对象
JavaScript 调试常见报错以及修复方法
js报错:Converting circular structure to JSON
网友评论