(RN)可能未处理的承诺拒绝(id:0):#Redux + axios

时间:2018-02-05 04:40:03

标签: reactjs react-native promise 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(...)。那么它不是一个函数 TypeError:_axios2.default.create(...)。然后不是函数

我基于f8 facebook app并将解析转换为休息

此代码有什么问题? 请帮忙..

1 个答案:

答案 0 :(得分:2)

axios.create会返回一个这样的实例:

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()