简体   繁体   English

未处理的拒绝(TypeError):无法读取未定义的属性“子级”

[英]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)

i am getting this error: 我收到此错误: 在此处输入图片说明

It looks like there are a couple of problems: 看来有两个问题:

  1. 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(将为truefalse ),并在in操作中使用该结果。 You need () around the in expression: !(category.parent in categoriesTree) 您需要in表达式中使用()!(category.parent in categoriesTree)

  2. Even with the revision, that checks for a property whose name is the value of category.parent in categoriesTree . 即使修改,即检查其名称是值的属性category.parentcategoriesTree 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.xrather 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.

相关问题 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined React-未处理的拒绝(TypeError):无法读取未定义的属性“ city” - React- Unhandled Rejection (TypeError): Cannot read property 'city' of undefined 未处理的拒绝(TypeError):无法读取未定义的 Stripe Javascript 的属性“id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined Stripe Javascript ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送” - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined 反应未处理的拒绝(TypeError):无法读取未定义的属性“状态” - React Unhandled Rejection (TypeError): Cannot read property 'state' of undefined 用于 React 的 geolib - 未处理的拒绝(TypeError):无法读取未定义的属性“getCenter” - geolib for React - Unhandled Rejection (TypeError): Cannot read property 'getCenter' of undefined 未处理的拒绝(TypeError):无法读取未定义 MERN 的属性“长度” - Unhandled Rejection (TypeError): Cannot read property 'length' of undefined MERN 未处理的拒绝(TypeError):无法读取未定义的属性“isUnAuthorizedError” - Unhandled Rejection (TypeError): Cannot read property 'isUnAuthorizedError' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性“替换” - Unhandled Rejection (TypeError): Cannot read property 'replace' of undefined axios:未处理的拒绝(TypeError):无法读取未定义的属性“错误” - axios: Unhandled Rejection (TypeError): Cannot read property 'error' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM