简体   繁体   English

ReactJS componentDidMount,获取Spotify API和Promise

[英]ReactJS componentDidMount, Fetch Spotify API and Promise

I'm learning how used ReactJS, Spotify API, and Promise. 我正在学习如何使用ReactJS,Spotify API和Promise。 I'm trying to fetch musician top albums on Spotify and play 30 seconds of the track. 我正在尝试在Spotify上获取音乐家热门专辑并播放30秒的曲目。

I'm using a Spotify package called spotify-web-api-node I think I'm not understanding something fundamental about React or JS. 我正在使用一个名为spotify-web-api-node的Spotify软件包,我想我不了解React或JS的基本内容。 Syntax error: Unexpected token, expected ( (11:8)

import React from 'react'; 从'react'导入React;

import SpotifyWebApi from 'spotify-web-api-node';
require('dotenv').config();


export default class SpotifyComponent extends React.Component {
  // Create the api object with the credentials
  const spotifyApi = new SpotifyWebApi({
    clientId : process.env.REACT_APP_SPOTIFY_CLIENT_ID,
    clientSecret : process.env.REACT_APP_SPOTIFY_CLIENT_SECRET
  });
// Save the access token so that it's used in future calls
  componentDidMount() {
    **(11:8)** --> return spotifyApi = new Promise((resolve, reject) => {
      spotifyApi.clientCredentialsGrant()

      .then( => (data) {
        console.log('The access token expires in ' + data.body['expires_in']);
        console.log('The access token is ' + data.body['access_token']);
      });

      // using Promises through Promise, Q or when - get Elvis' albums in range [20...29]
      spotifyApi.getArtistAlbums('43ZHCT0cAZBISjO8DG9PnE', {limit: 10, offset: 20})
        .then(function(data) {
          console.log('Album information', data);
        }, function(err) {
          console.error(err);
        });
    });

    SpotifyWebApi.setPromiseImplementation(Q);
  }
}

The way you're using the promises provided by the spotify-api are correct. 你使用spotify-api提供的承诺的方式是正确的。 However you shouldn't return a Promise from componentDidMount . 但是,您不应该从componentDidMount返回Promise React doesn't have any use for it. React没有任何用处。

Instead just run your promise based functions inside componentDidMount . 而只是在componentDidMount运行基于promise的函数。

componentDidMount() {

  // the next line will actually trigger the promise to run
  spotifyApi.clientCredentialsGrant()
    .then((data) => { // this line was missing "=>" in your original code
      console.log('The access token expires in ' + data.body['expires_in']);
      console.log('The access token is ' + data.body['access_token']);
    });

  // the next line also triggers a promise to run
  spotifyApi.getArtistAlbums('43ZHCT0cAZBISjO8DG9PnE', {limit: 10, offset: 20})
    .then(function(data) {
      console.log('Album information', data);
    }, function(err) {
      console.error(err);
    });
}

Also you can set Q as the promise provider right after your import. 您也可以在导入后立即将Q设置为承诺提供商。

import SpotifyWebApi from 'spotify-web-api-node';
SpotifyWebApi.setPromiseImplementation(Q);

You can't have a const definition inside of a class like that. 你不能在类这样的类中有一个const定义。

You should either move it outside or remove the const : 您应该将其移到外面或删除const

// Create the api object with the credentials
const spotifyApi = new SpotifyWebApi({
  clientId : process.env.REACT_APP_SPOTIFY_CLIENT_ID,
  clientSecret : process.env.REACT_APP_SPOTIFY_CLIENT_SECRET
});

export default class SpotifyComponent extends React.Component {

or 要么

export default class SpotifyComponent extends React.Component {
  // Create the api object with the credentials
  spotifyApi = new SpotifyWebApi({
    clientId : process.env.REACT_APP_SPOTIFY_CLIENT_ID,
    clientSecret : process.env.REACT_APP_SPOTIFY_CLIENT_SECRET
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM