【发布时间】:2020-06-12 14:44:05
【问题描述】:
我正在尝试从 Spotify 中提取用户的 Spotify 播放列表信息并将其显示在我正在创建的应用程序上。
这是我采取的步骤,有人可以帮我看看错误在哪里吗?
1) 在 Spotify.js 中添加一个方法
const Spotify = {
bringPlaylist() {
return fetch(`https://api.spotify.com/v1/me/playlists`)
.then(
response => {
if (response.ok) {
return response.json();
}
throw new Error("Request failed!");
},
networkError => {
console.log(networkError.message);
}
)
.then(jsonResponse => {
return jsonResponse;
});
}
};
2)将Spotify.js导入App.js,在App.js中创建一个方法调用bringSpotifylist,设置状态,绑定。
import Spotify from "../../util/Spotify.js";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
spotifyList: []
};
this.bringPlaylist = this.bringPlaylist.bind(this);
}
bringPlaylist() {
Spotify.bringPlaylist().then(spotifyList => {
this.setState({ spotifyList: spotifyList });
});
}
render() {
return (
<div>
<SpotifyPlaylist spotifyList={this.state.spotifyList} />
</div>
);
}
}
- 这里是我参考的Spotify API信息:https://developer.spotify.com/console/get-playlists/?user_id=wizzler
【问题讨论】:
-
当组件被挂载时你期望发生什么?您希望状态包含歌曲列表还是空数组?
-
@Gh05d 我希望状态包含播放列表。
标签: javascript reactjs get fetch jsx