[英]Unhandled Rejection (TypeError): Cannot read property 'children' of undefined
I am trying to fetch data from rest api using axios and i want to store data in an array and append new property children but i am getting error here is my code: 我正在尝试使用axios从rest api获取数据,我想将数据存储在数组中并追加新的属性子项,但是我在这里遇到错误:
var categoriesTree = {};
axios.get('https://akkar.market/wp-json/wp/v2/job_listing_category?per_page=100')
.then(response => {
categoriesTree = response.data
categoriesTree.forEach(category => {
if (category.parent === 0) {
if (!(category.id in categoriesTree)) {
categoriesTree[category.id] = {'name': category.name, 'children': []};
} else {
categoriesTree[category.id]['name'] = category.name;
}
} else {
if (!category.parent in categoriesTree) {
categoriesTree['parent'] = {'name': '', 'children': []};
}
categoriesTree['parent']['children'].push({'name': category.name, 'id': category.id});
}
})
})
console.log(categoriesTree)
It looks like there are a couple of problems: 看来有两个问题:
In !category.parent in categoriesTree
, you'd taking the logical NOT of category.parent
(which will be true
or false
) and using that result in the in
operation. 在
!category.parent in categoriesTree
,您将采用category.parent
的逻辑NOT(将为true
或false
),并在in
操作中使用该结果。 You need ()
around the in
expression: !(category.parent in categoriesTree)
您需要
in
表达式中使用()
: !(category.parent in categoriesTree)
Even with the revision, that checks for a property whose name is the value of category.parent
in categoriesTree
. 即使修改,即检查其名称是值的属性
category.parent
在categoriesTree
。 But then you create a property called parent
, not a property with the name from category.parent
. 但是随后您创建了一个名为
parent
的属性,而不是名称来自category.parent
的属性。
If the property is supposed to be the value of category.parent
, use that instead of 'parent'
: 如果该属性应该是
category.parent
的值,请使用该属性代替'parent'
:
if (!(category.parent in categoriesTree)) {
categoriesTree[category.parent] = {'name': '', 'children': []};
}
categoriesTree[category.parent]['children'].push({'name': category.name, 'id': category.id});
If it's supposed to be called parent
(literally), then use that consistently instead: 如果应该将其称为
parent
(字面意思),则应始终使用它:
if (!("parent" in categoriesTree)) {
categoriesTree.parent = {'name': '', 'children': []};
}
categoriesTree.parent['children'].push({'name': category.name, 'id': category.id});
If categoriesTree
will either not have a property called x
or the property will have an object reference as a value, you can use if (!categoriesTree.x
) rather than
if (!("x" in categoriesTree))`. 如果
categoriesTree
将不具有名为x
的属性,或者该属性将具有对象引用作为值,则可以使用if (!categoriesTree.x
) rather than
if(!(categoryTree中的“(x”))`。 Applying that to the two options above: 将其应用于以上两个选项:
if (!categoriesTree[category.parent])) {
categoriesTree[category.parent] = {'name': '', 'children': []};
}
categoriesTree[category.parent]['children'].push({'name': category.name, 'id': category.id});
If it's supposed to be called parent
(literally), then use that consistently instead: 如果应该将其称为
parent
(字面意思),则应始终使用它:
if (!categoriesTree.parent)) {
categoriesTree.parent = {'name': '', 'children': []};
}
categoriesTree.parent['children'].push({'name': category.name, 'id': category.id});
I'm not sure, what you mean under if (!category.parent in categoriesTree)
, but it looks like it always will return false
. 我不确定
if (!category.parent in categoriesTree)
下的含义,但看起来它总是会返回false
。 Try to change this condition simply to if (!categoriesTree.parent)
, and I suppose it will work. 尝试简单地将此条件更改为
if (!categoriesTree.parent)
,我想它会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.