Реагировать - отправить повара ie на Express - PullRequest
0 голосов
/ 30 апреля 2020

Я получаю токен для доступа к конечной точке API и хочу отправить этот токен моему серверному приложению (expressJS) для получения данных.

У меня есть следующее для моего приложения реакции:

export default class Account extends React.Component {
    constructor() {
        super();
        this.state = {
          token: null,
          response: {

          }
        };
        this.getCurrentlyPlaying = this.getCurrentlyPlaying.bind(this);
      }
      componentDidMount() {
        // Set token
        let _token = hash.access_token;
        if (_token) {
          this.setState({
            token: _token
          });
          const cookies = new Cookies();
          cookies.set('token', _token, { path: '/' });
          console.log(cookies.get('token'));
          this.getCurrentlyPlaying(_token);
        }
      }

      getCurrentlyPlaying() {
        fetch(`http://localhost:3001/account`)
        .then(res => res.json())
        .then(data => {
          this.setState ({
              response: data
          })
          console.log(data);
        });
      }
    render() {
       if (this.state.response[0].is_playing  === true) {
        return (
          <p> Something is playing</p>
         );
       }
       else {
         return (
          <p> Nothing is playing</p>
         );
       }
    }
}

В моем приложении express у меня получен повар ie, но я не уверен, действительно ли это получение повара ie созданного приложением реаги:

  router.get('/account', (req, res) => {
    const config = {
      headers: {
        'Authorization': `Bearer ${req.session.token}`
      }
    };
    fetch(`${CONFIG.spotifyUrl}/me/player/currently-playing `, config)
    .then(html => html.json())
    .then(json => {
      res.json(json);
    });
  });
module.exports = router;

Может кто-нибудь сказать мне, где я иду не так, пожалуйста?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...