javascript - 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);
}
解决方案
我建议使用另一个then
而不是使用finally
. then
之后catch
就像一个finally
. 不要忘记catch
在你的承诺链中至少使用一个,以处理你的指令失败。
所以这两行代码是一样的:
api.get(…).then(…).catch(…).then(...)
和
api.get(…).then(…).catch(…).finally(...)
推荐阅读
- python - Flask:如何根据按钮输入更新sqlite上的信息
- node.js - 如何在我的 cocod2d-x (C++) 游戏中集成 Node.js 服务器
- python - 关于添加“数组”元素的Python问题
- package - 安装不平衡学习的环境问题
- ssl - 为多个 Traefik 服务共享 Acme 配置
- linux - 使用 Makefile 遍历目录
- sqlalchemy - SQLAlchemy - 如何使每个 ForeignKey 关系的列唯一?
- tomcat - 通过 context.xml 禁用一些 http 方法
- java - WildFly 中的 RestEasy UTF-8 响应
- automotive - 如何在 AGL 中显示弹出窗口或警报?