我是使用React的新手,正在尝试调用Spotify API以获取播放列表。
在运行代码时,我只会不断遇到解析错误,但我不太清楚为什么。我在下面评论的那条线上找到了它。谁能帮忙,这是下面的代码:
import React, { Component } from 'react';
import './App.css';
import SpotifyWebApi from 'spotify-web-api-js';
const spotifyApi = new SpotifyWebApi();
class App extends Component {
constructor(){
super();
const params = this.getHashParams();
console.log(params)
const token = params.access_token;
if (token) {
spotifyApi.setAccessToken(token);
}
this.state = {
loggedIn: token ? true : false,
nowPlaying: { name: 'Not Checked', albumArt: '' }
}
}
getHashParams() {
var hashParams = {};
var e, r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
e = r.exec(q)
while (e) {
hashParams[e[1]] = decodeURIComponent(e[2]);
e = r.exec(q);
}
return hashParams;
}
getUserPlaylists(){
spotifyApi.getUserPlaylists('amy.greene')
.then(function(data) {
console.log('Retrieved playlists', data.body);
},function(err) {
console.log('Something went wrong!', err);
});
}); // <----------- Error points here
// **************************************************
render() {
return (
<div className="App">
<a href='http://localhost:8888' > Login to Spotify </a>
<div>
{ this.state.loggedIn &&
<button onClick={() => this.getUserPlaylists()}>
Your Playlists
</button>
}
</div>
</div>
);
}
}
export default App;