简体   繁体   English

无法显示从后端作为对象接收的数据,

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

相关问题 Javascript:临时存储和显示从后端接收到的图像作为二进制数据? - Javascript: Temporarily store and display image received from backend as binary data? 如何显示在角度/ typescript UI处从后端收到的Java地图数据? - How to display java map data received from backend at angular /typescript UI? 在 ReactJS 中更改从后端接收的数据格式 - Changing the Data Format that is received from the backend in ReactJS 无法显示在HTML上接收到的JSON数据 - Unable to Display JSON Data Received on HTML 如何解析从后端接收的错误并作为 javascript 中的错误 object 分派 - How to parse an error received from a backend and dispatched as an Error object in javascript 使用angularJS将数据(从后端接收)从html发送到另一个 - Sending data (received from backend) from on html to another with angularJS 显示从保管箱值接收的数据 - Display data received from drop box value React,在componentDidMount()中执行GET后无法显示api接收的数据 - React, unable to display the data received by an api, after doing a GET in componentDidMount() 无法将数据从输入传递到后端 - Unable to Pass Data to Backend from Input 无法以角度 2 显示来自后端的 JSON 数据 - cannot display JSON data from the backend in angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM