I have following response that I get in fetch()
{
"errorCode": "00",
"errorMsg": "success",
"roleList": [
{
"role_id": 900,
"roleDescription": "Role_I"
},
{
"role_id": 901,
"roleDescription": "Role_II"
}
]
}
Now i need to get roleDescription and render it in , So i tried traversing through the JSON and storing the roleDescription in an array in state.
below is the method written for the same:
getroles(e) {
fetch('http://xyz', {
method: 'POST',
body: JSON.stringify({
"role_id": this.props.location.state.role_id
})
})
.then((response) => response.json())
.then((responseJson) => {
console.log(`response of getroles: `, responseJson)
if (responseJson.errorCode === '00') {
this.setState({roleList : JSON.stringify(responseJson.roleList)});
let temp = [];
for (var i=0; i < JSON.stringify(responseJson.roleList).length; i++) {
temp.push(JSON.stringify(responseJson.roleList[i].roleDescription))
}
}
else {
alert("Error Fetching roles" + responseJson.errorMsg);
}
})
.catch((error) => {
console.error(error);
});
}
but i am getting error
Cannot read property 'roleDescription' of undefined
on line temp.push
I am very new to react so I am not quite sure if I have the right approach. Please help.
roleListin the temp array?