I am retrieving data from a REST service. The data comes back as an array. This is the function I use to retrieve the data.
export function fetchMessage() {
return function(dispatch) {
axios.get(`${ROOT_URL}/currencies`, {
headers: { token: localStorage.getItem('token') }
})
.then(response => {
console.log(response.data);
dispatch({
type: FETCH_MESSAGE,
payload: response.data
});
});
}
}
The output from fetchMessage is an array of Objects.
[Object, Object, Object, Object, Object, Object]
0:Object
addedDate:"2013-08-22"
ccyCode:"CHF"
country:"SCHWIZER FRANC"
lastChangeDate:"2016-05-02"
offerRate:7.02
prevRate:8.501
prevRateDate:"2016-04-01"
rate:8.425
rateDate:"2016-05-01"
unit:1
__proto__:Object
1:Object
2:Object
3:Object
4:Object
5:Object
The fetchMessage function is called by the component below.
class Feature extends Component {
componentWillMount() {
this.props.fetchMessage();
//console.log(this.props);
}
render() {
return (
<div>{this.props.message}</div>
);
}
}
function mapStateToProps(state) {
return { message: state.auth.message };
}
export default connect(mapStateToProps, actions)(Feature);
The component can't render the message because it is an
bundle.js:1256 Uncaught (in promise) Error: Objects are not valid as a React child (found: object with keys {ccyCode, country, unit, rateDate, rate, prevRateDate, prevRate, offerRate, addedDate, lastChangeDate}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of `Feature`.(…)
I've tried to run a map of the this.props.message like this
<div>
{this.props.message.map(
function(object){
return ('Something'
);
}
)}
</div>
But I get an error message saying that I can't run map on message. How can I render the data in the object? Do I need to save it in some other way? How do I iterate over the objects to render them?
<div>{this.props.message.ccyCode} {this.props.message.rate}</div>and go from there.<div>{this.props.message.ccyCod}</div>and<div>{this.props.message.rate}</div>and both gives the error messageUncaught TypeError: Cannot read property 'rate' of undefinedthe undefined property of course change toccyCodewhen I try withccyCode.