ネストされた配列の "レコード"をプルすることを目標としています。私の現在の出力は反応コンソール内の配列を表示しますが、エラーがあります。私は可能な限り簡潔にしようと努力しますが、物事を短く保ちます。オブジェクト内にネストされた配列をマップ内に返します。
エラー画面に_getRecordsを参照している3行がありますので、_getRecordsは問題の子です。
class RecordBox extends Component {
constructor() {
super();
this.state = {
showComments: false,
results: []
};
}
render(){
const records = this._getRecords();
return (
// jsx template code...
);
}
// API Call
_fetchRecords() {
$.ajax({
method: 'GET',
url: 'http://apidata:8888/data.json',
success: (results) => {
this.setState({ results });
},
error:() => {
console.log('error');
}
});
}
_getRecords() {
// TypeError: this.state.results.map is not a function...
return this.state.results.map((record) => {
return <Comment body={record["Contractor Name"]} />
});
}
}
私は以下のようなフィードを持っています。私はこれを変更する許可がありません。
{
"result": {
"records": [
{
"id": 1,
"email": "[email protected]",
"author": "Clu",
"Contractor Name": "Just say no to love!!",
"avatarUrl": "https://placeimg.com/200/240/any"
},{
"id": 2,
"email": "[email protected]",
"author": "Anne Droid",
"Contractor Name": "I wanna know what love is...",
"avatarUrl": "https://placeimg.com/200/240/any"
}
]
}
}
私はES6に新しいですし、私と一緒に裸してくださいようにも反応します。
なぜ、_getRecordsがクラスの外にありますか? – VivekN
'this.state.results'には何を保存していますか? – BravoZulu
@arkjosephどのように_getRecordsをどこから呼び出すのですか? – VivekN