svrcekmichal / redux-axios-middleware

Redux middleware for fetching data with axios HTTP client
MIT License
920 stars 96 forks source link

How to call async method #36

Closed JakubKontra closed 7 years ago

JakubKontra commented 7 years ago

I'm using your great middleware! Thanks guys but:

when:

GameModal.react.js

async doCreateGame() {
    const {
        fields,
        hideModal,
        formDisabled,
    } = this.props;

    if (formDisabled) return;

    try {
        const response = await createGame(fields.$values());
        console.log(response);

    } catch (error) {
        console.log(error);
    }
}

Actions.js

export function createGame(fields) {
    return {
        types: ['CREATE_GAME','CREATE_GAME_SUCCESS','CREATE_GAME_ERROR'],
        payload: {
        request: {
            url: '/api/game',
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                game_name: fields.game_name,
                game_token: fields.game_token + "1-gtkoen",
                destination_url: fields.destination_url
            }
        }
        }
    }
}

AxiosMiddlewareOptions

const axiosMiddlewareOptions = {
    interceptors: {
    request: [
        (action, config) => {
            if (action.getState().auth.accessToken) {
                config.headers['x-access-token'] = action.getState().auth.accessToken
            }

        return config
        }
    ]
    }
}

axiosMiddleware(client, axiosMiddlewareOptions),

It returns this:

{
    payload: {
    request: {
        data {
            destination_url: 'url',
            game_name: 'gname',
            game_token: 'token'
        },
        headers {
            Content-Type: 'application/json',
        }
    }
    },
    types: ['CREATE_GAME', 'CREATE_GAME_SUCCESS', 'CREATE_GAME_ERROR']
}

My question is: Where is my x-auth-token and why i dont get response from url? Thanks :)

JakubKontra commented 7 years ago

@svrcekmichal man please.. :) :/

svrcekmichal commented 7 years ago

@JakubKontra Hi, to second comment, I'm quite busy last two weeks and I'm finishing my deadlines till christmas so you have three options as this is OS project. First is that you can wait for us to have time for your issue, because we are having our own projects deadlines and family and free time etc. Second is that you can try and fix your problem yourself, then create PR and we will see how we can improve that and third option is to try other packages maybe you will find something that will work for you. Actually, there is also fourth option, to create new package but that's quite costly.

So back to the issue, you've got doCreateGame calling createGame, but what is createGame? is it bind to store.dispatch? How? Can you show that? Because I believe that's issue, the response you showed is action which should be passed to dispatch and it would run through middleware

nmaves commented 7 years ago

I agree, I looks like you create an action here createGame(fields.$values()) but you never actually dispatch it. You are logging the response but that is just the plain old action object.

nmaves commented 7 years ago

Closing this as there has not been a response for 3 months.