我收到未捕获( promise 中)TypeError:无法读取未定义的属性“setState”
。我正在尝试使用 this.setState
渲染一些轨道,但仅限于 9 个轨道,想看看是否一切都正确。
这里的代码如下:
import React from 'react';
require('dotenv').config();
// My Steps: //
// 1. Want to fetch for KD Rusha Top Ablums on Spoitify Done
// 2. Limit them to only 9 tracks
// 3. Add play button on top of each track to play 30 sec of the Song
// GO TO SPOTIFY AND GETTING KD RUSHA TOP ABLUMS lIMIT TO 9 SONGS
// WHEN YOU CLICK ON TOP OF THE ABLUMS A PLAY BUTTON ICON WILL SHOW TO INDICATE THAT YOU CAN PLAY 30 SECONDS OF SONG
export default class SpotifyComponent extends React.Component {
constructor(props){
super(props);
this.state = {
artist: null,
};
}
componentWillMount() {
var request = new Request('https://api.spotify.com/v1/artists/5JLWikpo5DFPqvIRi43v5y/', {
method: 'GET',
headers: new Headers({
'Accept': 'application/json',
'Content-Type': 'text/plain',
'Authorization': 'Access Token Here'
})
});
fetch(request)// fetch the Spotify date from KD Rusha Account
.then(function(response){
var result = response.json()
this.setState({// State artist albums
albums: result,
selectedTrack: [0]
});
console.log(this.setState);
if (response.status >= 400) {
throw new Error("Bad response from server");
}
return response.json();
});
}
render(){
return(
<ul className="spotify-cantainer">
</ul>
);
}
}
最佳答案
您将需要使用 .bind
或粗箭头语法 =>
来声明您的 Promise 回调。
关于javascript - Spotify 网络 API : Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44339296/