简体   繁体   中英

Undefined imported function in React Native?

I'm working with API and React Native currently and just stumbled across a TypeError. I have 2 files, one for the screen that gets search query, and another file, api.js , that handles the search request to the API. I'm exporting a function from the api.js file and importing it into the search query file, but when I try to call it, I get a TypeError TypeError: (0, _api.findMovie) is not a function. (In '(0, _api.findMovie)(movieName)', '(0, _api.findMovie)' is undefined) TypeError: (0, _api.findMovie) is not a function. (In '(0, _api.findMovie)(movieName)', '(0, _api.findMovie)' is undefined) . Why is that?

Search.js

import { findMovie } from "../api";

export default class Search extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      value: ""
    };
  }

  getMovieName = async () => {
    const movieName = await this.state.value;
    const movieResult = await findMovie(movieName);
  };
  render() {
    return (
      <View>
        <Button title="Search" onPress={this.getMovieName} />
      </View>
    );
  }
}

api.js

const processMovie = movie => ({
  movie: movie.Title,
  starring: movie.Actors,
  year: movie.Year,
  country: movie.country,
  rating: movie.imdbRating
});

export const findMovie = async name => {
  const response = await fetch(
    `http://www.omdbapi.com/?apikey=myapikey&t=${name}`
  );
  const results = [await response.json()];
  console.log(results);
  return results.map(processMovie);
};

The Search.js file is located in /components, whereas api.js lies within the root directory. If any more information is needed, please let me know. Thanks in advance.

I have done minor changes in api.js. You may try them

export function findMovie = async (name) => {
  const response = await fetch(
    `http://www.omdbapi.com/?apikey=myapikey&t=${name}`
  );
  const results = await response.json();
  console.log(results);
  return results.map(processMovie);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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