【问题标题】:Possible Unhandled Promise Rejection (id: 9): TypeError: undefined is not an object When upload react native axios可能未处理的 Promise Rejection (id: 9): TypeError: undefined is not an object 当上传反应原生 axios
【发布时间】:2021-04-29 01:31:06
【问题描述】:

我正在尝试以 react-native 上传,但多次尝试后都没有运气。

有人请帮我解决这个问题吗?非常感谢

const handleChoosePicture = () => {
    launchImageLibrary({ noData: true }, (response) => {
        if (response) {
            setChoosePicture(response);
        }
    });
}


const createFormData = (photo) => {
    const data = new FormData();
    data.append('files', {
        name: photo.fileName,
        type: photo.type,
        uri: Platform.OS === 'ios' ? photo.uri.replace('file://', '') : photo.uri,
    });
    return data;
};

const uploadImage = async (choosePicture) => {
    const Token = await AsyncStorage.getItem('member_token');
    await axios.post(
        `${AppConfig.apiUrl}/media/upload`,
        createFormData(choosePicture),
        {
            headers: {
                Accept: 'application/json',
                'Content-Type': 'multipart/form-data',
                authorization: `Bearer ${Token}`,
            },
        })
        .then((response) => {
            console.log('response', response);
        })
        .catch((error) => {
            console.log('error', error);
        });
}

可能的未处理承诺拒绝(id:9):

TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[17], "axios").AxiosInstance.post')

【问题讨论】:

    标签: react-native react-native-image-picker


    【解决方案1】:

    我认为您只需要重新启动打包程序即可。 如果您正在运行 Metro-packager 并安装新的依赖项,则需要重新启动打包程序,以便它将新的依赖项添加到 dependencyMap。

    [更新]

    您还可以检查您的某些库是否与您的 react-native 版本不兼容。我刚刚和我的一个朋友谈过,他和你有同样的问题(dependencyMap),但是对于自定义组件,问题实际上是旧版本中的 react-native-gesture-handler

    【讨论】:

    • 感谢您的评论,我已经重启了很多次,但一点运气都没有。
    • 你能展示一下你是如何导入 axios 的吗?确保您正在导入:import axios from 'axios'
    • @NamApple 您还可以检查您的某些库是否与您的 react-native 版本不兼容。我刚刚和我的一个朋友谈过,他和你有同样的问题(dependencyMap),但是对于自定义组件,问题实际上是旧版本中的 react-native-gesture-handler。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多