【发布时间】:2019-05-21 09:34:18
【问题描述】:
这是我的 JSON
[
{
"id": 1,
"job_id": 1,
"company_profile": "Sales and Marketing",
"company_about": "Established in 1992 , it is a renouned marketing company",
"company_product": "Ford,Mustang,Beetle",
"key_skills": "commmunication,english,spanish,german",
"qualification": "High School,Masters",
"job_description": "Must be a Local of Mumbai",
"created_at": null,
"updated_at": null
}
]
我正在尝试获取它的值。 这是我记录它们的反应代码。
public getJobDetails = (jobid: number) => {
const JobId = jobid;
fetch('http://127.0.0.1:8000/api/jobs/detail/' + JobId)
.then(response => response.json())
.then(
responseJson => {
console.log(responseJson);
this.setState({ details: responseJson });
},
() => {
console.log(this.state.details);
}
)
.catch(error => {
console.error(error);
});
}
public render() {
const { details } = this.state;
console.log(details);
console.log(details[0]);
console.log(details[0]) 返回
{id: 1, job_id: 1, company_profile: "Sales and Marketing", company_about: "Established in 1992 , it is a renouned marketing company", company_product: "Ford,Mustang,Beetle", …}
但是为什么 console.log(details[0].company_profile) 返回 undefined ??? 它给出的错误是:
TypeError:无法读取未定义的属性“company_about”
谁能帮忙??
【问题讨论】:
-
当你试图访问一些复杂的对象时,看看是否已经填充了状态。例如在第一次
renderthis.state.details 可能是undefined,因此出现此错误。 -
是的,在第一次渲染期间它是未定义的?但后来它变得定义?我仍然收到此错误。
-
也添加一些有意义的字符串到你的
console.logs,看看它是如何呈现的。 -
我试过 JSON.stringify ,但仍然未定义:(
标签: json reactjs typescript