Search code examples
javascriptreact-nativemultipartform-data

why do I get formdata is read-only warning when trying to clear the formdata in react native


Im trying to send formdata to an api by using Axios. When the user gets an error after sending the request, the formdata should be cleared. But I got Unhandled promise rejection: TypeError: "formdata" is read-only when trying to clear the data in formdata.

    const formdata = new FormData();
    const fetchData = () => {
        allImage.forEach((file) => {
            formdata.append('files[]', file);
        });
        axios({
            method: 'post',
            url: URL,
            headers: {
                'Content-Type': "multipart/form-data", 
            },
            data: formdata,
        }).then(function (result) { 
            if(result.data.message === "Receipt Successfully added") {
                Alert.alert("Upload Successufull", result.data.message);
                navigation.goBack()
            }

        }).catch(function (error) {
            if(error.response.status == 413) {
                Alert.alert("Error", "The attached file(s) is too large.")
                formdata = new FormData();
            }
        });
    }


Solution

  • FormData is a constant, it should be a let