简体   繁体   English

在 React Native 中未定义导入的 function?

[英]Undefined imported function in React Native?

I'm working with API and React Native currently and just stumbled across a TypeError.我目前正在使用 API 和 React Native,只是偶然发现了一个 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.我有 2 个文件,一个用于获取搜索查询的屏幕,另一个文件api.js用于处理对 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) 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) . TypeError: (0, _api.findMovie) is not a function. (In '(0, _api.findMovie)(movieName)', '(0, _api.findMovie)' is undefined) Why is that?这是为什么?

Search.js搜索.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 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. Search.js 文件位于 /components 中,而 api.js 位于根目录中。 If any more information is needed, please let me know.如果需要更多信息,请告诉我。 Thanks in advance.提前致谢。

I have done minor changes in api.js.我对 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);
};

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

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