I have a React Native API call I am making.
Theoretically it should work -
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);
});
}
However I am getting the following error:
What is causing this error? The code looks valid.
Here is what get is:
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);
}
The "then is not a function" error occurs when the then() method is called on a value that is not a promise. To solve the error, convert the value to a promise before calling the method or make sure to only call the then() method on valid promises.
In summary: then : when a promise is successful, you can then use the resolved data. catch : when a promise fails, you catch the error, and do something with the error information. finally : when a promise settles (fails or passes), you can finally do something.
catch... finally statements combo handles errors without stopping JavaScript. The try statement defines the code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result.
A catch -block contains statements that specify what to do if an exception is thrown in the try -block. If any statement within the try -block (or in a function called from within the try -block) throws an exception, control is immediately shifted to the catch -block.
I suggest to use another then
instead of using finally
. then
after catch
is works like a finally
. don't forget to use at least one catch
in your promise chain, in order to handle your instructions failure.
So this two line of code is the same:
api.get(…).then(…).catch(…).then(...)
and
api.get(…).then(…).catch(…).finally(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With