【问题标题】:Possible Unhandled Promise Rejection (id: 2): TypeError: _petition.default.getCategory is not a function可能未处理的 Promise Rejection (id: 2): TypeError: _petition.default.getCategory is not a function
【发布时间】:2020-07-31 15:27:59
【问题描述】:

我在 YellowBox 中有这个问题 (反应原生)

可能的未处理承诺拒绝(id:2): TypeError: _petition.default.getCategory 不是函数 TypeError: _petition.default.getCategory 不是函数 在 api$ (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:104932:33) 在 tryCatch (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:26922:19) 在 Generator.invoke [as _invoke] (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27098:24) 在 Generator.prototype 处。 [下一个] (http://localhost:8081/index.bundle?........

我的请愿书代码如下:

class petition {
  async getCategories() {
    try {
      const response = await fetch('https://yts.mx/api/v2/list_movies.json');
      const data = await response.json();
      return data.data.movies;
    } catch (error) {
      console.log(`error ocurred:${error}`);
    }
  }
  async getRecommendations(id) {
    try {
      const response = await fetch(
        `https://yts.mx/api/v2/movie_suggestions.json?movie_id=${id}`,
      );
      const rawData = await response.json();
      return rawData.data.movies;
    } catch (error) {
      console.log(`error ocurred (${error})`);
    }
  }
}

export default new petition();

【问题讨论】:

  • 我看不到任何getCategory 函数或函数调用。这是在哪里定义和调用的?
  • 记得从 catch 处理程序中重新抛出错误,否则它们将保持被捕获并且调用者将返回一个即使发生错误也会遵循其成功路径的承诺。更好的是,不要陷入低级工作者功能。将所有捕获/记录留给更高级别。
  • export default new petition(); .... 奇怪的模式 - 有什么原因吗?

标签: javascript reactjs react-native promise


【解决方案1】:

好的,我有一个额外的函数调用,我的错误很抱歉,谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2020-03-17
    • 2020-01-06
    相关资源
    最近更新 更多