【问题标题】:React - Accessing nested objects in returned dataReact - 访问返回数据中的嵌套对象
【发布时间】:2017-07-07 20:25:25
【问题描述】:

我正在使用fetch 从本地 API 返回一些数据。我能够返回数据并设置状态。但是,在我的render() 函数中,当使用map 并尝试访问比数据中的顶级对象更深的任何内容时,我收到undefined 错误。我可以正确地将我想要的任何级别的数据记录到控制台,但我无法在呈现的组件中访问它。

constructor(props) {
  super(props);
  this.state ={
    verifications: []
  }
}

componentWillMount(){
  fetch('http://localhost:3001/verifications')
    .then(response => response.json())
    .then((verifications) => {
      this.setState({verifications})
      console.log(this.state);
    });
}

在我的渲染中

{this.state.verifications.map(verification =>
  <div key={verification._id}>
    <ReviewListItem
      hasAvatar={true}
      imageUrl={verification.supplier.logo}
      title={verification.supplier.companyName}
      description={verification.tasks.length}
    />
  </div>
)}

这是我得到的错误: Unhandled Rejection (TypeError): Cannot read property 'logo' of null

我已经到处寻找这个问题的答案,但我觉得我一定是在接近这个错误。我来自 Angular 1,我对 React 非常陌生。也许我很难掌握这些概念。

这是我的数据:

[
{
"_id": 1000,
"supplier": {
"_id": 1000,
"companyName": "ACME Business Ventures",
"logo": "/images/logos/suppliers/acme.jpg",
"avettaId": "ADS83J",
"brandColor": "#563E5E",
"clients": [],
"locations": [],
"primaryContact": {},
"address": {},
"createdAt": "2017-06-30T17:42:23.479Z"
},
"tasks": [
{
"form": "PQF General",
"question": "How long have you been in business?",
"answer": "18 Years",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "PQF General",
"question": "Have you had any fatalities in the last 3 years?",
"answer": "No",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "Diversity Questionnaire",
"question": "Are you a minority-owned business?",
"answer": "Yes",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "Sustainability Questionnaire",
"question": "What percentage of your business runs on renewable energy?",
"answer": "About 55%, but that's expected to climb to 80% by next year.",
"status": "incomplete",
"attachment": "",
"comments": []
}
]
},
{
"_id": 2000,
"supplier": null,
"tasks": [
{
"form": "PQF General",
"question": "How long have you been in business?",
"answer": "18 Years",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "PQF General",
"question": "Have you had any fatalities in the last 3 years?",
"answer": "No",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "Diversity Questionnaire",
"question": "Are you a minority-owned business?",
"answer": "Yes",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "Sustainability Questionnaire",
"question": "What percentage of your business runs on renewable energy?",
"answer": "About 55%, but that's expected to climb to 80% by next year.",
"status": "incomplete",
"attachment": "",
"comments": []
}
]
},
{
"_id": 3000,
"supplier": null,
"tasks": [
{
"form": "PQF General",
"question": "How long have you been in business?",
"answer": "18 Years",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "PQF General",
"question": "Have you had any fatalities in the last 3 years?",
"answer": "No",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "Diversity Questionnaire",
"question": "Are you a minority-owned business?",
"answer": "Yes",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "Sustainability Questionnaire",
"question": "What percentage of your business runs on renewable energy?",
"answer": "About 55%, but that's expected to climb to 80% by next year.",
"status": "incomplete",
"attachment": "",
"comments": []
}
]
},
{
"_id": 4000,
"supplier": null,
"tasks": [
{
"form": "PQF General",
"question": "How long have you been in business?",
"answer": "18 Years",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "PQF General",
"question": "Have you had any fatalities in the last 3 years?",
"answer": "No",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "Diversity Questionnaire",
"question": "Are you a minority-owned business?",
"answer": "Yes",
"status": "incomplete",
"attachment": "",
"comments": []
},
{
"form": "Sustainability Questionnaire",
"question": "What percentage of your business runs on renewable energy?",
"answer": "About 55%, but that's expected to climb to 80% by next year.",
"status": "incomplete",
"attachment": "",
"comments": []
}
]
}
]

【问题讨论】:

  • verification.supplier 是一个数字,而不是一个对象。看起来您尝试访问的那些字段不存在于数据中的任何位置
  • 啊废话...哎呀。发布我的种子文件。编辑以显示实际数据。

标签: javascript json reactjs


【解决方案1】:

您的一个供应商为空(至少一个)。考虑使用 imageUrl={verification.supplier ? verification.supplier.logo : null} 之类的东西,而不是 imageUrl={verification.supplier.logo}

【讨论】:

  • 嗯……废话。 :-( 请原谅我,我在角落里撅嘴。这太荒谬了。我想先看看整个物体是值得的。我从来没有超过第一个,只是假设其余的都在那里。谢谢大家的帮助!
  • 类似的事情发生在我身上很多。
【解决方案2】:

数组的第二个索引是supplier: null。您可以先检查供应商是否存在

{this.state.verifications.map(verification =>
  <div key={verification._id}>
    <ReviewListItem
      hasAvatar={true}
      imageUrl={verification.supplier && verification.supplier.logo}
      title={verification.supplier && verification.supplier.companyName}
      description={verification.tasks.length}
    />
  </div>
)}

【讨论】:

    【解决方案3】:

    供应商在您的 json sn-p 中显然为空:

    "supplier": null,
    

    因此它显然失败了:)

    我相信 Angular 会自动检查如下表达式中的空值:foo.bar.baz.something.deeply.nested——因此,如果任何内容为空,则结果将为空——在反应中(以及一般的 js 中),你需要自己检查空值。

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多