jsonをフェッチするときに異なる応答が表示されるのはなぜですか?私は矢印機能を使用すると動作し、そうでない場合は動作しません。矢印出力の有無にかかわらず、矢印出力機能を持つ別の出力をフェッチする
constructor(props){
super(props);
this.state = {
data: [],
};
this.url = 'https://fcctop100.herokuapp.com/api/fccusers/top/recent';
}
矢印機能を取得:
fetch(url)
.then((response) => {
return response.json()
}).then((json) => {
this.setState({data: json});
console.log('parsed json', json)
}).catch((ex) => {
console.log('parsing failed', ex)
});
コンソール上で戻り値:
parsed json Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 90 more… ]
をそして、私は矢印の機能を使用しないときに出力が異なります。
fetch(url)
.then((response) => {
return response.json()
}).then(function(json) {
this.setState({data: json});
console.log('parsed json', json)
}).catch((ex) => {
console.log('parsing failed', ex)
});
返品:
parsing failed TypeError: this is undefined Stack trace:
listCampers/<@http://localhost:3000/static/js/bundle.js:18177:17