【问题标题】:(RN) Possible Unhandled Promise Rejection (id: 0): #Redux + axios(RN)可能的未处理承诺拒绝(id:0):#Redux + axios
【发布时间】:2018-07-14 22:03:39
【问题描述】:
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 应用程序并将解析转换为休息

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

【问题讨论】:

  • axios.create 给你一个新的实例你确定你不是在寻找these 实例方法之一
  • requestService().get(url);是那种方法,不是吗?

标签: reactjs react-native promise axios


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 2018-03-11
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2016-11-24
    相关资源
    最近更新 更多