Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript api.get(…).then(…).catch(…).finally不是函数_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript api.get(…).then(…).catch(…).finally不是函数

Javascript api.get(…).then(…).catch(…).finally不是函数,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在进行一个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 .g

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

在get函数中传递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);

     get(api + "/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);
      });
  }
从“../../utils/API”导入API;
componentDidMount(){
让merchantId=this.props.merchant.id;
让api=新api(this.props.gatheredTokens);
让自我=这个;
api.setRetry(10);
获取(api+“/merchantMessages”,{repl_str:merchantId})
.then(response=>this.merchantMessageConfiguration(response.data))
.catch(函数(错误){
console.log(错误);
})
.最后(函数(){
self.state.list.push(
);
})
.最后(函数(){
自售商品通知(self);
});
}
只有本机承诺(使用
新承诺构建)才能保证有
。最后
方法(在较新的环境中)。(在较旧的环境中,
。最后
与使用
新承诺创建的承诺完全不起作用

axios似乎没有在内部使用
newpromise
,而是返回一个thenable,它不保证有
finally
方法(因为它没有,所以会抛出一个错误)

虽然您可以使用显式Promise构造反模式将axios调用包装在本机
新Promise
中,使其在原型链中具有
Promise.prototype.最后
,但更好的选择()是只使用
Promise.resolve
,这将使thenable成为本地承诺,同时保留thenable的失败或成功:

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

承诺应该是es6的
finally
,我不确定这是否得到
axios
承诺的支持,如果可以的话,我建议使用
then
而不是
finally

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);
  })
  .then(function () {
    self.state.list.push(
      <Card
        merchant={self.props.merchant}
        key={self.props.merchant.id}
        bubblemsg={self.state.bubblemsg}
      />
    );
  })
  .then(function () {
    self.merchantNoticeLoading(self);
  });
 }
从“../../utils/API”导入API;
componentDidMount(){
让merchantId=this.props.merchant.id;
让api=新api(this.props.gatheredTokens);
让自我=这个;
api.setRetry(10);
应用程序编程接口
.get(“商品消息”,{repl_str:merchantId})
.then(response=>this.merchantMessageConfiguration(response.data))
.catch(函数(错误){
console.log(错误);
})
.然后(函数(){
self.state.list.push(
);
})
.然后(函数(){
自售商品通知(self);
});
}

我曾经遇到过类似的问题,但这是Firefox独有的问题,但在解决或拒绝问题后,我仍然需要编写一些代码,因此我使用了
async/await
表单

从“../../utils/API”导入API;
componentDidMount(){
让merchantId=this.props.merchant.id;
让api=新api(this.props.gatheredTokens);
让自我=这个;
api.setRetry(10);
(异步()=>{
试一试{
const response=await api.get(“merchantMessages”,{repl_str:merchantId})
this.merchantMessageConfiguration(response.data)
}捕获(错误){
console.log(错误);
}最后{
self.state.list.push(
);
}  
})()
}

我建议先使用另一个
然后使用
,而不是最后使用
然后
之后
捕获
就像是一个
最后
一样工作。不要忘记在你的承诺链中至少使用一个
catch
,以处理你的指令失败

所以这两行代码是相同的:

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

var url='1〕https://api.github.com/users/hadley/orgs';
fetchJson(url)
.然后((obj)=>{
console.log('obj')
控制台日志(obj)
console.log('obj')
})
.catch((错误)=>{
console.log('错误')
console.log(错误)
console.log('错误')
})
.最后(()=>{

console.log('whalhello那里没有
。get
返回一个实现
方法的类似承诺的对象。finally
方法?听起来好像没有(在支持
。最后
的环境中,它只能保证存在于使用
新承诺构建的本机承诺上,我相信这是一个非常不幸的混淆源)Get只是axios调用的包装器。我将添加它。显式承诺构造反模式可以解决它,但我打赌还有更好的方法way@nivendha.get是一种API@CertainPerformance-哦,最终有具体的环境规则吗?我已经根据你在问题末尾给出的get函数回答了n、 您必须导入get函数并在组件中使用它。use
require('promise.prototype.finally')
如果您需要finally支持,
finally
函数将添加到
promise.prototype
中。然后()
之后。执行进入
.catch()时不会调用catch()
。有什么解决方案吗?这是可行的。但是,如果我正在为现代浏览器开发一个现代应用程序,并且这个方法在MDN中有文档记录,为什么我不能使用finally()。@Sadegh Teimori
。然后()
块(作为finally)在
之后使用。当执行进入
.catch()时,不会调用catch()
。有什么解决方案吗?@Sadegh你为什么建议这样做?在现代浏览器中,
.finally()
有问题吗?
.finally()
不受所有浏览器的支持。为了确保我的代码在所有浏览器上都有效,我总是使用
.catch()
后跟
.then()
,而不是
.finally()
。您仍然应该避免。若要将一个thenable强制为本机承诺,请编写
返回承诺.resolve(axios.get(…);
,仅此而已。哦,所以
承诺。resolve
将允许拒绝通过而不会引起问题,非常酷!谢谢
api.get(…).then(…).catch(…).then(...)
api.get(…).then(…).catch(…).finally(...)