I want to call an API using react Js,where JSON data is not in an array
when am running this code am getting an error:this.props.result.map is not a function.
The API which am calling is not an array its just JSON object
here is my code:
var App = React.createClass({
//setting up initial state
getInitialState:function(){
return{
data:[]
};
},
getDataFromServer:function(URL){
$.ajax({
type:"GET",
dataType:"json",
url:URL,
headers:{
'Content-Type':'application/json',
},
success: function(response) {
this.showResult(response);
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
showResult: function(response) {
this.setState({
data: JSON.parse(response)
});
},
var Result = React.createClass({
render:function(){
var result = this.props.result.map(function(result,index){
return <ResultItem key={index} user={ result } />
});
return(<div className="row">
<div className="col-md-12">
<table className="table table-bordered">
<thead>
<tr>
<th className="col-md-4">Id</th>
<th >Name</th>
<th>Independent</th>
<th>Description</th>
<th>uuid</th>
</tr>
</thead>
<tbody>
{result}
</tbody>
</table>
</div>
</div>)
}
});
var ResultItem = React.createClass({
render:function(){
var camper = this.props.user;
return(
<tr >
<td>{camper.id}</td>
<td>{camper.name}</td>
<td>{camper.independent}</td>
<td>{camper.description}</td>
<td>{camper.uuid}</td>
</tr>
);
}
});