[英]Unable to display data received as object from backend ,
I am fetching data from the backend, it has a particular sub-part called headers which further has an additional name:value pairs . 我正在从后端获取数据,它具有一个称为标头的特定子部分,该子部分还具有一个附加的名称:值对。 like this -
data : { headers : {name:"Nishant", value:"random" } }
, I want to display all the fields inside a modal , When I try to use Object.keys()
, this error is thrown - "cannot convert undefined or null to object". 像这样
data : { headers : {name:"Nishant", value:"random" } }
,我想显示模式中的所有字段,当我尝试使用Object.keys()
,抛出此错误- “无法将未定义或null转换为对象”。
render(){
return(
let request_data = this.props.head // has the entire data
let data = request_data.headers //contains the headers section to be displayed.
<div className="modal-body">
<div className="row">
<div className="col-lg-6">
{
<div>
{// want to display the headers data here }
{ (request_data.body !== '') ? <div>Body : {request_data.body}</div> : ''}
</div>
}
</div>
</div>
</div>
);
}
Not Sure what to do as I tried Using Object.keys
but an error is thrown saying "cannot convert undefined or null to object". 不确定我尝试使用
Object.keys
该怎么办,但抛出错误,提示“无法将未定义或null转换为对象”。 Although by Using JSON.stringify() I am getting the result but not in the expected look and feel. 虽然通过使用JSON.stringify()可以得到结果,但没有达到预期的外观。
在<pre>
标签中呈现文本并使用JSON.stringify
<pre>{JSON.stringify(request_data.body,null,2)}</pre>
If a valid response is received. 如果收到有效的响应。 For Example:
例如:
{
"data": {
"headers": {
"name": "Nishant",
"value": "random"
}
}
}
var responseData = JSON.parse('{ "data": { "headers": { name: "Nishant", value: "random" } } }')
The first option will display the value in data object and the second will display the value inside the headers object. 第一个选项将在数据对象中显示值,第二个选项将在标头对象中显示值。
<pre>responseData.data</pre>
<pre>responseData.data.headers</pre>
Check header is not undefined before get values; 在获取值之前,检查头不是未定义的;
<div className="col-lg-6">
{
<div>
{request_data.header ? Object.values(request_data.header).map(value => value) : null}
{(request_data.body !== '') ? <div>Body : {request_data.body}</div> : ''}
</div>
}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.