我正在进行一个React原生API调用.

从理论上讲,它应该有效-

    import API from "../../utils/API";

  componentDidMount() {
    let merchantId = this.props.merchant.id;
    let api = new API(this.props.gatheredTokens);
    let self = this;
    api.setRetry(10);
    api
      .get("merchantMessages", { repl_str: merchantId })
      .then(response => this.merchantMessageConfiguration(response.data))
      .catch(function (error) {
        console.log(error);
      })
      .finally(function () {
        self.state.list.push(
          <Card
            merchant={self.props.merchant}
            key={self.props.merchant.id}
            bubblemsg={self.state.bubblemsg}
          />
        );
      })
      .finally(function () {
        self.merchantNoticeLoading(self);
      });
  }

然而,我得到了以下错误:

TypeError

是什么导致了这个错误?代码看起来有效.

下面是get的要点:

 get(API, params = this.defaultParams) {
    this.call = "GET";
    let constructedURL = this.constructURL(API, params);
    axiosRetry(axios, { retries: this.retry });
    return axios.get(constructedURL, this.config);
  }

推荐答案

我建议用then而不是finally.catch之后的then就像finally.不要忘记在你的promise 链中至少使用一个catch,以便处理你的失败指示.

所以这两行代码是一样的:

api.get(…).then(…).catch(…).then(...)

api.get(…).then(…).catch(…).finally(...)

React-native相关问答推荐

使用下拉菜单时react 本机react-native-dropdown-select-list,它会将下面的项目向下移动.如何阻止这种情况发生

react-native 根据状态隐藏显示按钮

应用程序主体尚未注册

react-native 签名 [AppName]Tests 需要开发团队 ios

如何在 react-native 中设置 Onpress On Textinput

React Native 用于低互联网带宽的小型 apk APP应用

React Redux 使用带有连接组件的 HOC

使用 .env 变量在 react-native 应用程序上设置 appcenter

library not found for -lRCTGeolocation

React native Refresh 有效,但下一次调用仍使用最后一个令牌

React-Native 构建失败的应用程序:mergeDebugAssets

React Native:无法点击调试菜单

在配置 react-native-maps 中获取 "supportLibVersion" 、 "playServicesVersion" 和 "androidMapsUtilsVersion" 值

带有 xcode 模拟器的 react-native 有非常慢的alert

如何在不使用 JSX 的情况下在 React Native 中制作组件?

将 React Native 升级到 0.60-RC2 后找不到库libjsc.so

React-Native 最低 Android API 级别

降级 react-native 的适当机制

react-native构建错误:package android.support.annotation does not exist

如何将图形 API 与 react-native-fbsdk 一起使用?