简体   繁体   English

Spotify Web API:未捕获(承诺)TypeError:无法读取未定义的属性“setState”

[英]Spotify Web API: Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined

I am getting Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined .我收到Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined I'm trying to using this.setState to so rendering some tracks onto but limit to only 9 tracks want to see if doing everything correctly.我正在尝试使用this.setState来渲染一些轨道,但仅限于 9 个轨道,想看看是否一切都正确。

Here Code Below:这里的代码如下:

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.bind箭头语法=>来声明您的promise回调。

.then(function(response){ .then(function(response){

Should be .then((response) => { 应该是.then((response)=> {

If you don't use arrow functions you lose this context. 如果不使用箭头功能,则会丢失此上下文。

When you are in the then the context of this changes and no longer refers to the component. 当你在then的背景下this变化,不再是指该组件。 You have a couple of options to resolve this. 您有两种选择可以解决此问题。

You could assign this to a variable and then use the variable: eg 你可以指定this一个变量,然后使用变量:如

var self = this;

request(...)
.then(function(response) {
   self.setState(...)
})

Or, if you are able to with your application you could use arrow functions instead of normal functions. 或者,如果您能够使用自己的应用程序,则可以使用箭头功能代替常规功能。 eg 例如

request(...)
.then((response) => {
   this.setState(...)
})

The reason for this is arrow function, by default bind to the scope. 原因是箭头功能,默认情况下绑定到范围。 You can read more here: https://toddmotto.com/es6-arrow-functions-syntaxes-and-lexical-scoping/#functionality-lexical-scoping-this 您可以在此处阅读更多信息: https : //toddmotto.com/es6-arrow-functions-syntaxes-and-lexical-scoping/#functionality-lexical-scoping-this

this generally happens when we get the response code 403 which means forbidden according to Spotify's API documentation.这通常发生在我们收到响应代码403时,根据 Spotify 的 API 文档,这意味着禁止。 to solve this problem just add the scope of the response you want to get in the authentication URL and it will start working.要解决此问题,只需在身份验证 URL 中添加您想要获得的响应的 scope 即可开始工作。

暂无
暂无

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

相关问题 未捕获的TypeError:无法读取未定义的属性&#39;setState&#39; - Uncaught TypeError: Cannot read property 'setState' of undefined 未捕获到的TypeError:无法使用函数读取未定义的属性“ setState” - Uncaught TypeError: Cannot read property 'setState' of undefined with a function 反应:未捕获的TypeError:无法读取未定义的属性&#39;setState&#39; - React: Uncaught TypeError: Cannot read property 'setState' of undefined 未捕获的类型错误:无法读取 Component.setState 处未定义的属性“enqueueSetState” - Uncaught TypeError: Cannot read property 'enqueueSetState' of undefined at Component.setState React&Firebase-未捕获的TypeError:无法读取未定义的属性&#39;setState&#39; - React & firebase- uncaught TypeError: Cannot read property 'setState' of undefined ReactJS:未捕获的TypeError:无法读取未定义的属性“ setState” - ReactJS: Uncaught TypeError: Cannot read property 'setState' of undefined React - 未捕获的类型错误:无法读取未定义的属性“setState” - React - uncaught TypeError: Cannot read property 'setState' of undefined 未捕获(承诺中):TypeError:无法读取未定义的属性“userSubject”类型错误:无法读取未定义的属性“userSubject” - Uncaught (in promise): TypeError: Cannot read property 'userSubject' of undefined TypeError: Cannot read property 'userSubject' of undefined 类型错误:在使用 spotify 的 api 时无法读取未定义的属性 - TypeError: Cannot read property of undefined while using spotify's api 未捕获(承诺)TypeError:无法读取未定义的属性&#39;uid&#39; - Uncaught (in promise) TypeError: Cannot read property 'uid' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM