遇到错误“操作必须是普通对象。使用自定义中间件进行异步操作”

时间:2019-08-27 11:58:42

标签: reactjs react-native redux react-redux

我已经看过像this这样的线程,但这似乎并不能解决我的业务问题。

以下是3种相关方法。

const createFormData = post => {
    const {
        title,
        body,
        image
    } = post;

    const data = new FormData();

    data.append(
        "body",
        JSON.stringify({
            title,
            body,
            mediaType: image.type
        })
    );

    console.log(data); // prints

    return data;
};

const submitMedia = (data, replace) => async dispatch => {
    console.log('reached'); // never prints

    dispatch({
        type: SUBMITTING
    });

    try {
        const response = await axios.post(
            `http://${GATEWAY}:5000/api/uploads/single`,
            data
        );

        dispatch({
            type: SUBMISSION_PENDING
        });

        replace("NewSubmission", {
            name: "NewSubmission"
        });
    } catch (err) {
        // TODO test this
        dispatch({
            type: SUBMISSION_FAIL,
            payload: err.response.data
        });

        alert("Uh oh", "Something went wrong");
        console.log("Error caught", err);
    }
};

const handleSubmit = async () => {
    try {
        const post = {
            title,
            body,
            image
        };

        const data = createFormData(post);

        console.log('reached'); // never prints

        submitMedia(data, replace);
    } catch (err) {
        console.log("err caught --> ", err);
    }
};

我的程序似乎正确构建了表单数据,但是几乎好像它从未退出createFormData(post)方法。不确定从这里开始,我尝试按照this answer的建议将方法包装为闭包,但没有骰子。我什至尝试将异步移出handleSubmit(),但仍然没有。不知道我在做什么错。

0 个答案:

没有答案