Closed nigel-daniels closed 6 years ago
As an update it seems that the issue is not so much the 'disappearing' promise, as far as I can tell the middleware component of redux-pack
is consuming this, but the Promise returned by the fetch API is not being handled correctly for some reason?
As I understood from the documentation for redux-pack
when building the action the return from the call to the API should be returning a promise. The ReadMe shows the following:
// actions.js
export function loadFoo(id) {
return {
type: LOAD_FOO,
promise: Api.getFoo(id),
};
}
So in my own code I am doing this (debug removed for clarity):
export const login = (email, password) => {
return {
type: types.LOGIN,
promise: services.login(email, password)
};
};
However I suspect the returned Promise from the fetch call in my API (services) function is not being handled as a Promise for some reason? I end up that call with:
return fetch(request)
.then(checkResponse)
.then(response => response.json())
.catch(error => error);
The documentation for the fetch API states:
The fetch() method of the WindowOrWorkerGlobalScope mixin starts the process of fetching a resource from the network. This returns a promise that resolves to the Response object representing the response to your request.
So am I wondering if:
redux-pack
is struggling to handle the Promise returned by the fetch API for some internal reason?Any guidance or suggestions welcome, if the answer is 1 then maybe I can contribute something using fetch()
to the documentation to help future users, if it's 2 then let me know if there is any diagnostics I can run to help out.
Closed as I finally noticed the import of handle to be incorrect, it was missing the braces.
I'm new to using redux-pack and it looks like a great way to simplify my code and make it more maintainable. However I have hit an issue where the handler throws and error and I cannot figure out why this is, I wanted to check if there is a bug in the handler/dispatch set up somewhere?
My code looks like the following and is instrumented with debug to figure out what is going on:
action_types.js
auth_actions.js
auth_services.js
auth_reducer.js
When I hit the submit button on my Login.jsx component I see the following on the console:
Console Output
So I see the Login method gets called, the state has the expected values, the dispatch is called, then the action calls the service and build the request with the expected data. The action then returns a Promise Object. The reducer is then called with the expected state but I note the action appears to have the correct type (
LOGIN
) but no apparent promise??? The type maps to the correct switch case then the handler throws the error above. In fact it throws two of the same type and then I get a404
response from the server which has some how been called with aGET
(not aPOST
) to/login?
.So I guess my question is what's causing this? Is the dispatch stripping the promise or is the action formed correctly and I'm hitting an issue in the handler?
Let me know if more information is needed to help diagnose or if you need me to try out anything.