1

I've looked around threads like this one, but it doesn't seem to help with my rather bizare issue.

Here are the 3 relevant methods.

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);
    }
};

My program seems to build the form data properly, but it's almost as if it never exits createFormData(post) method. Not sure here to go from here, I've tried wrapping the method as a closure as this answer suggests, but no dice. I've even tried moving the async out of handleSubmit(), still nothing. Not sure what I'm doing wrong.

Mike K
  • 7,621
  • 14
  • 60
  • 120
  • First, are you sure you have properly installed and configured [redux-thunk](https://github.com/reduxjs/redux-thunk)? Then, could you show how you call `handleSubmit `? – Pierre V. Aug 27 '19 at 14:30
  • I got it working by moving `createFormData` out of `client/src/utils/utils.js`. Also, I'd hope `redux-thunk` is properly configured.. I mean my other dispatched actions work just fine, just as does the method `submitMedia` (exported from `src/actions/media.js`) which is used by `handleSubmit`. Any idea why moving `createFormData` out of `utils.js` and directly writing it in the same file as `handleSubmit` got the program working? I decided to pass it off as something to potentially deal with later. I've been staring at the screen for 11 hours today. – Mike K Aug 27 '19 at 16:15

0 Answers0