简体   繁体   English

Axios在React组件中返回未定义

[英]Axios returning undefined in React component

Essentially I have a input field and a submit button. 本质上,我有一个输入字段和一个提交按钮。 My goal is to have the input field be what is requested in the axios request. 我的目标是使输入字段成为axios请求中要求的内容。

This is the code for my axios request: 这是我的axios请求的代码:

import axios from 'axios';

module.exports = {
  getItunesArtists: function(ARTIST_NAME) {
    var encodedURI = window.encodeURI('https://itunes.apple.com/search?term=' + ARTIST_NAME);

    return axios.get(encodedURI).then(function(response) {
      console.log('response.data.items', response.data.items);
    });
  },
};

The console spits back console吐回来

response.data.items undefined

And this is my main component and a functional component. 这是我的主要组件和功能组件。

import React, { Component } from 'react';
import api from '../utils/api';

import axios from 'axios';

// prettier-ignore
function Albums(props) {
  return (
    <ul>
      {props.albums.map(function(album, index) {
        return (
          <li key={album.name} >

            <ul >
              <li>
               {console.log(album)}
              </li>

            </ul>
          </li>
        );
      })}
    </ul>
  );
}

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = { albums: [], artistSearched: '', dirty: false };
    this.handleSearchTermChange = this.handleSearchTermChange.bind(this);
    this.getAlbums = this.getAlbums.bind(this);
  }

  componentDidMount() {
    this.getAlbums(this.state.artistSearched);
  }

  getAlbums(artist) {
    this.setState(function() {
      return {
        artistSearched: artist,
        albums: [],
      };
    });
    api.getItunesArtists(artist).then(
      function(artists) {
        this.setState(function() {
          return {
            albums: artists,
          };
        });
      }.bind(this)
    );
  }

  handleSearchTermChange(event) {
    console.log(event.target.value);
    this.setState({ artistSearched: event.target.value });
  }

  render() {
    const { albums, artistSearched } = this.state;

    return (
      <div>
        <h1>Itunes Album Fetcher</h1>
        <form style={{ marginTop: '20px' }}>
          <input
            onChange={this.handleSearchTermChange}
            value={this.state.artistSearched}
            className="form-control"
            placeholder="Enter album name"
          />
          <button type="submit" disabled={!artistSearched}>
            Search
          </button>
        </form>
        {!this.state.albums ? <p>Loading</p> : <Albums albums={this.state.albums} />}
      </div>
    );
  }
}

Thank you in advance! 先感谢您!

You're console.logging response.data.items , but when I call this API I see the format of the response is different: 您是console.logging response.data.items ,但是当我调用此API时,我看到响应的格式是不同的:

{
 "resultCount":50,
 "results": [ ... ]
}

You want to use response.data.results 您想使用response.data.results

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

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