(RN) Possible Unhandled Promise Rejection (id: 0): #Redux + axios
(RN) Possible Unhandled Promise Rejection (id: 0): #Redux + axios
function requestService() {
return axios.create({
baseURL: userEndpoint,
headers: {
common: {
Accept: 'application/json',
}
}
}).then(response => {
return response.json();
}).catch(error => {
console.log('requestService', error);
});
}
module.exports = {
requestService
};
RequestService.js
import type { PromiseAction } from "./Types";
async function loadHopses(userId: number): PromiseAction {
const url = `hopses/user/${userId}`
const list = requestService().get(url);
await InteractionManager.runAfterInteractions();
return {
type: "LOADED_HOPSES",
list
};
}
module.exports = {
loadHopses
};
Action.js
this.props.dispatch(loadHopses(1));
App.js
export type PromiseAction = Promise<Action>;
Types.js
错误是
可能未处理的承诺拒绝(id:0):
TypeError: _axios2.default.create(...).then 不是一个函数
TypeError: _axios2.default.create(...).then 不是函数
我基于 f8 facebook 应用程序并将解析转换为 rest
这段代码有什么问题?
请帮助..
axios.create returns 像这样的实例:
var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
这些是唯一有效的方法。
axios#request(config)
axios#get(url[, config])
axios#delete(url[, config])
axios#head(url[, config])
axios#options(url[, config])
axios#post(url[, data[, config]])
axios#put(url[, data[, config]])
axios#patch(url[, data[, config]])
您可以这样使用它:
return axios.({
method: 'post',
baseURL: userEndpoint,
headers: {
common: {
Accept: 'application/json',
}
}
}).then(...).catch(...);
使用 axios()
代替 axios.create()
function requestService() {
return axios.create({
baseURL: userEndpoint,
headers: {
common: {
Accept: 'application/json',
}
}
}).then(response => {
return response.json();
}).catch(error => {
console.log('requestService', error);
});
}
module.exports = {
requestService
};
RequestService.js
import type { PromiseAction } from "./Types";
async function loadHopses(userId: number): PromiseAction {
const url = `hopses/user/${userId}`
const list = requestService().get(url);
await InteractionManager.runAfterInteractions();
return {
type: "LOADED_HOPSES",
list
};
}
module.exports = {
loadHopses
};
Action.js
this.props.dispatch(loadHopses(1));
App.js
export type PromiseAction = Promise<Action>;
Types.js
错误是 可能未处理的承诺拒绝(id:0): TypeError: _axios2.default.create(...).then 不是一个函数 TypeError: _axios2.default.create(...).then 不是函数
我基于 f8 facebook 应用程序并将解析转换为 rest
这段代码有什么问题? 请帮助..
axios.create returns 像这样的实例:
var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
这些是唯一有效的方法。
axios#request(config)
axios#get(url[, config])
axios#delete(url[, config])
axios#head(url[, config])
axios#options(url[, config])
axios#post(url[, data[, config]])
axios#put(url[, data[, config]])
axios#patch(url[, data[, config]])
您可以这样使用它:
return axios.({
method: 'post',
baseURL: userEndpoint,
headers: {
common: {
Accept: 'application/json',
}
}
}).then(...).catch(...);
使用 axios()
代替 axios.create()