首页 > 解决方案 > api.get(...).then(...).catch(...).finally 不是函数

问题描述

我正在进行一个 React Native 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);
      });
  }

但是我收到以下错误:

类型错误

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

这是得到的:

 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);
  }

标签: javascriptreactjsreact-native

解决方案


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

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

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

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

推荐阅读