dispatch is not a function in Server Side Rendering
i have a problem with redux-thunk, it just doesn't pass the dispatch to my action creator, i'm loggin it but it is still undefined:
here is my action creator:
import * as types from './actionTypes'
import Service from '../service'
export const fetchFromApi = payload =>
return type: types.FETCH_FROM_API_TEST, payload
export const fetchApiTestAction = () => dispatch =>
return Service.GetUsers().then( _data =>
dispatch(fetchFromApi( _data.data )) // you can even distructure taht like fetchFromApi(( data ))
).catch ( err =>
// error catching here
// you can even call an failure action here...
// but sorry about it , i have no time for that , im sorry :(
throw (err)
)
here is my Service js class
// Basic fetch
async basicFetch(url, options, headers = this.authernticateHeader)
let fetchOptions =
if(options.headers)
fetchOptions =
mode: 'cors',
cache: 'default',
...options
else
fetchOptions =
mode: 'cors',
cache: 'default',
...options,
headers
// log before sending the request...
// console.log(url, fetchOptions, fetchOptions.headers.get('Content-Type') )
try
const response = await fetch(
`$this.serverAddress$url`,
...fetchOptions
)
if(!response.ok)
throw
message: response.statusText,
api: `$options.method $this.serverAddress`
const responseJson = await response.json()
console.log('----------------------------')
console.log(responseJson)
console.log('----------------------------')
return responseJson
catch(err)
throw ...err
GetUsers = () =>
return this.basicFetch(this.urls.GET_USERS,
method: 'GET'
)
here is my routes .js file
import fetchApiTestAction from './actions/fetchApiTestAction'
const routes = [
path: '/',
exact: true,
component: Home,
fetchInitialData: fetchApiTestAction()
,
path: '/about',
component: About
,
path: '/contact',
component: Contact
,
]
and here is my server.js file
app.get('/*', (req, res, next) => )
i havent faced with this kind of problem so far. when i console.log(dispatch)
inside of my action creator(before return
part) it is undefined. i should say that here is what my terminal shows to me:
Server is listening on port 8000...
----------------------------
page: 1,
per_page: 3,
total: 12,
total_pages: 4,
data:
[ id: 1,
first_name: 'George',
last_name: 'Bluth',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg' ,
id: 2,
first_name: 'Janet',
last_name: 'Weaver',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg' ,
id: 3,
first_name: 'Emma',
last_name: 'Wong',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg' ]
----------------------------
TypeError: dispatch is not a function
at eval (webpack-internal:///./src/js/actions/fetchApiTestAction.js:17:7)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
this means that im getting the data from api but why dispatch is not a function ????
javascript reactjs redux react-redux redux-thunk
add a comment |
i have a problem with redux-thunk, it just doesn't pass the dispatch to my action creator, i'm loggin it but it is still undefined:
here is my action creator:
import * as types from './actionTypes'
import Service from '../service'
export const fetchFromApi = payload =>
return type: types.FETCH_FROM_API_TEST, payload
export const fetchApiTestAction = () => dispatch =>
return Service.GetUsers().then( _data =>
dispatch(fetchFromApi( _data.data )) // you can even distructure taht like fetchFromApi(( data ))
).catch ( err =>
// error catching here
// you can even call an failure action here...
// but sorry about it , i have no time for that , im sorry :(
throw (err)
)
here is my Service js class
// Basic fetch
async basicFetch(url, options, headers = this.authernticateHeader)
let fetchOptions =
if(options.headers)
fetchOptions =
mode: 'cors',
cache: 'default',
...options
else
fetchOptions =
mode: 'cors',
cache: 'default',
...options,
headers
// log before sending the request...
// console.log(url, fetchOptions, fetchOptions.headers.get('Content-Type') )
try
const response = await fetch(
`$this.serverAddress$url`,
...fetchOptions
)
if(!response.ok)
throw
message: response.statusText,
api: `$options.method $this.serverAddress`
const responseJson = await response.json()
console.log('----------------------------')
console.log(responseJson)
console.log('----------------------------')
return responseJson
catch(err)
throw ...err
GetUsers = () =>
return this.basicFetch(this.urls.GET_USERS,
method: 'GET'
)
here is my routes .js file
import fetchApiTestAction from './actions/fetchApiTestAction'
const routes = [
path: '/',
exact: true,
component: Home,
fetchInitialData: fetchApiTestAction()
,
path: '/about',
component: About
,
path: '/contact',
component: Contact
,
]
and here is my server.js file
app.get('/*', (req, res, next) => )
i havent faced with this kind of problem so far. when i console.log(dispatch)
inside of my action creator(before return
part) it is undefined. i should say that here is what my terminal shows to me:
Server is listening on port 8000...
----------------------------
page: 1,
per_page: 3,
total: 12,
total_pages: 4,
data:
[ id: 1,
first_name: 'George',
last_name: 'Bluth',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg' ,
id: 2,
first_name: 'Janet',
last_name: 'Weaver',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg' ,
id: 3,
first_name: 'Emma',
last_name: 'Wong',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg' ]
----------------------------
TypeError: dispatch is not a function
at eval (webpack-internal:///./src/js/actions/fetchApiTestAction.js:17:7)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
this means that im getting the data from api but why dispatch is not a function ????
javascript reactjs redux react-redux redux-thunk
Why would it be a function in the first place? You are runningfetchInitialData
even before you configure the redux-store...dispatch
is a property of the redux-store, if you don't have a redux-store, then you can'tdispatch
... Also, notice thatactiveRoute.fetchInitialData
is not a thunk... If it was a thunk it would be dispatched.
– Josep
Nov 12 at 10:52
add a comment |
i have a problem with redux-thunk, it just doesn't pass the dispatch to my action creator, i'm loggin it but it is still undefined:
here is my action creator:
import * as types from './actionTypes'
import Service from '../service'
export const fetchFromApi = payload =>
return type: types.FETCH_FROM_API_TEST, payload
export const fetchApiTestAction = () => dispatch =>
return Service.GetUsers().then( _data =>
dispatch(fetchFromApi( _data.data )) // you can even distructure taht like fetchFromApi(( data ))
).catch ( err =>
// error catching here
// you can even call an failure action here...
// but sorry about it , i have no time for that , im sorry :(
throw (err)
)
here is my Service js class
// Basic fetch
async basicFetch(url, options, headers = this.authernticateHeader)
let fetchOptions =
if(options.headers)
fetchOptions =
mode: 'cors',
cache: 'default',
...options
else
fetchOptions =
mode: 'cors',
cache: 'default',
...options,
headers
// log before sending the request...
// console.log(url, fetchOptions, fetchOptions.headers.get('Content-Type') )
try
const response = await fetch(
`$this.serverAddress$url`,
...fetchOptions
)
if(!response.ok)
throw
message: response.statusText,
api: `$options.method $this.serverAddress`
const responseJson = await response.json()
console.log('----------------------------')
console.log(responseJson)
console.log('----------------------------')
return responseJson
catch(err)
throw ...err
GetUsers = () =>
return this.basicFetch(this.urls.GET_USERS,
method: 'GET'
)
here is my routes .js file
import fetchApiTestAction from './actions/fetchApiTestAction'
const routes = [
path: '/',
exact: true,
component: Home,
fetchInitialData: fetchApiTestAction()
,
path: '/about',
component: About
,
path: '/contact',
component: Contact
,
]
and here is my server.js file
app.get('/*', (req, res, next) => )
i havent faced with this kind of problem so far. when i console.log(dispatch)
inside of my action creator(before return
part) it is undefined. i should say that here is what my terminal shows to me:
Server is listening on port 8000...
----------------------------
page: 1,
per_page: 3,
total: 12,
total_pages: 4,
data:
[ id: 1,
first_name: 'George',
last_name: 'Bluth',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg' ,
id: 2,
first_name: 'Janet',
last_name: 'Weaver',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg' ,
id: 3,
first_name: 'Emma',
last_name: 'Wong',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg' ]
----------------------------
TypeError: dispatch is not a function
at eval (webpack-internal:///./src/js/actions/fetchApiTestAction.js:17:7)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
this means that im getting the data from api but why dispatch is not a function ????
javascript reactjs redux react-redux redux-thunk
i have a problem with redux-thunk, it just doesn't pass the dispatch to my action creator, i'm loggin it but it is still undefined:
here is my action creator:
import * as types from './actionTypes'
import Service from '../service'
export const fetchFromApi = payload =>
return type: types.FETCH_FROM_API_TEST, payload
export const fetchApiTestAction = () => dispatch =>
return Service.GetUsers().then( _data =>
dispatch(fetchFromApi( _data.data )) // you can even distructure taht like fetchFromApi(( data ))
).catch ( err =>
// error catching here
// you can even call an failure action here...
// but sorry about it , i have no time for that , im sorry :(
throw (err)
)
here is my Service js class
// Basic fetch
async basicFetch(url, options, headers = this.authernticateHeader)
let fetchOptions =
if(options.headers)
fetchOptions =
mode: 'cors',
cache: 'default',
...options
else
fetchOptions =
mode: 'cors',
cache: 'default',
...options,
headers
// log before sending the request...
// console.log(url, fetchOptions, fetchOptions.headers.get('Content-Type') )
try
const response = await fetch(
`$this.serverAddress$url`,
...fetchOptions
)
if(!response.ok)
throw
message: response.statusText,
api: `$options.method $this.serverAddress`
const responseJson = await response.json()
console.log('----------------------------')
console.log(responseJson)
console.log('----------------------------')
return responseJson
catch(err)
throw ...err
GetUsers = () =>
return this.basicFetch(this.urls.GET_USERS,
method: 'GET'
)
here is my routes .js file
import fetchApiTestAction from './actions/fetchApiTestAction'
const routes = [
path: '/',
exact: true,
component: Home,
fetchInitialData: fetchApiTestAction()
,
path: '/about',
component: About
,
path: '/contact',
component: Contact
,
]
and here is my server.js file
app.get('/*', (req, res, next) => )
i havent faced with this kind of problem so far. when i console.log(dispatch)
inside of my action creator(before return
part) it is undefined. i should say that here is what my terminal shows to me:
Server is listening on port 8000...
----------------------------
page: 1,
per_page: 3,
total: 12,
total_pages: 4,
data:
[ id: 1,
first_name: 'George',
last_name: 'Bluth',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg' ,
id: 2,
first_name: 'Janet',
last_name: 'Weaver',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg' ,
id: 3,
first_name: 'Emma',
last_name: 'Wong',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg' ]
----------------------------
TypeError: dispatch is not a function
at eval (webpack-internal:///./src/js/actions/fetchApiTestAction.js:17:7)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
this means that im getting the data from api but why dispatch is not a function ????
javascript reactjs redux react-redux redux-thunk
javascript reactjs redux react-redux redux-thunk
edited Nov 12 at 4:45
asked Nov 12 at 4:25
a_m_dev
1,14531228
1,14531228
Why would it be a function in the first place? You are runningfetchInitialData
even before you configure the redux-store...dispatch
is a property of the redux-store, if you don't have a redux-store, then you can'tdispatch
... Also, notice thatactiveRoute.fetchInitialData
is not a thunk... If it was a thunk it would be dispatched.
– Josep
Nov 12 at 10:52
add a comment |
Why would it be a function in the first place? You are runningfetchInitialData
even before you configure the redux-store...dispatch
is a property of the redux-store, if you don't have a redux-store, then you can'tdispatch
... Also, notice thatactiveRoute.fetchInitialData
is not a thunk... If it was a thunk it would be dispatched.
– Josep
Nov 12 at 10:52
Why would it be a function in the first place? You are running
fetchInitialData
even before you configure the redux-store... dispatch
is a property of the redux-store, if you don't have a redux-store, then you can't dispatch
... Also, notice that activeRoute.fetchInitialData
is not a thunk... If it was a thunk it would be dispatched.– Josep
Nov 12 at 10:52
Why would it be a function in the first place? You are running
fetchInitialData
even before you configure the redux-store... dispatch
is a property of the redux-store, if you don't have a redux-store, then you can't dispatch
... Also, notice that activeRoute.fetchInitialData
is not a thunk... If it was a thunk it would be dispatched.– Josep
Nov 12 at 10:52
add a comment |
1 Answer
1
active
oldest
votes
That won't work unless you pass the redux-store dispatch
property when you invoke fetchInitialData
... Something like this:
const _promise = activeRoute.fetchInitialData
? activeRoute.fetchInitialData(store.dispatch)
: Promise.resolve()
The real problem is that your redux-store doesn't even exist at that point, because you are configuring it after that promise has finished... So that's something else that you will have to address.
first off, thank you for your respond, but how can i configure my store that it could be available for server side at the time that server needs to dispatch an action to fetch data from an endPoin??? thatfetchInitialData
ends up in my service.js config file , how that could be possible to instantiate store in my service.js file? how could it possible?
– a_m_dev
Nov 12 at 12:45
you are a masterpiece , take care of your self , and many many many thanks for answering me the god damn right answer. love you...
– a_m_dev
Nov 12 at 20:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255959%2fdispatch-is-not-a-function-in-server-side-rendering%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
That won't work unless you pass the redux-store dispatch
property when you invoke fetchInitialData
... Something like this:
const _promise = activeRoute.fetchInitialData
? activeRoute.fetchInitialData(store.dispatch)
: Promise.resolve()
The real problem is that your redux-store doesn't even exist at that point, because you are configuring it after that promise has finished... So that's something else that you will have to address.
first off, thank you for your respond, but how can i configure my store that it could be available for server side at the time that server needs to dispatch an action to fetch data from an endPoin??? thatfetchInitialData
ends up in my service.js config file , how that could be possible to instantiate store in my service.js file? how could it possible?
– a_m_dev
Nov 12 at 12:45
you are a masterpiece , take care of your self , and many many many thanks for answering me the god damn right answer. love you...
– a_m_dev
Nov 12 at 20:25
add a comment |
That won't work unless you pass the redux-store dispatch
property when you invoke fetchInitialData
... Something like this:
const _promise = activeRoute.fetchInitialData
? activeRoute.fetchInitialData(store.dispatch)
: Promise.resolve()
The real problem is that your redux-store doesn't even exist at that point, because you are configuring it after that promise has finished... So that's something else that you will have to address.
first off, thank you for your respond, but how can i configure my store that it could be available for server side at the time that server needs to dispatch an action to fetch data from an endPoin??? thatfetchInitialData
ends up in my service.js config file , how that could be possible to instantiate store in my service.js file? how could it possible?
– a_m_dev
Nov 12 at 12:45
you are a masterpiece , take care of your self , and many many many thanks for answering me the god damn right answer. love you...
– a_m_dev
Nov 12 at 20:25
add a comment |
That won't work unless you pass the redux-store dispatch
property when you invoke fetchInitialData
... Something like this:
const _promise = activeRoute.fetchInitialData
? activeRoute.fetchInitialData(store.dispatch)
: Promise.resolve()
The real problem is that your redux-store doesn't even exist at that point, because you are configuring it after that promise has finished... So that's something else that you will have to address.
That won't work unless you pass the redux-store dispatch
property when you invoke fetchInitialData
... Something like this:
const _promise = activeRoute.fetchInitialData
? activeRoute.fetchInitialData(store.dispatch)
: Promise.resolve()
The real problem is that your redux-store doesn't even exist at that point, because you are configuring it after that promise has finished... So that's something else that you will have to address.
answered Nov 12 at 11:01
Josep
9,75623137
9,75623137
first off, thank you for your respond, but how can i configure my store that it could be available for server side at the time that server needs to dispatch an action to fetch data from an endPoin??? thatfetchInitialData
ends up in my service.js config file , how that could be possible to instantiate store in my service.js file? how could it possible?
– a_m_dev
Nov 12 at 12:45
you are a masterpiece , take care of your self , and many many many thanks for answering me the god damn right answer. love you...
– a_m_dev
Nov 12 at 20:25
add a comment |
first off, thank you for your respond, but how can i configure my store that it could be available for server side at the time that server needs to dispatch an action to fetch data from an endPoin??? thatfetchInitialData
ends up in my service.js config file , how that could be possible to instantiate store in my service.js file? how could it possible?
– a_m_dev
Nov 12 at 12:45
you are a masterpiece , take care of your self , and many many many thanks for answering me the god damn right answer. love you...
– a_m_dev
Nov 12 at 20:25
first off, thank you for your respond, but how can i configure my store that it could be available for server side at the time that server needs to dispatch an action to fetch data from an endPoin??? that
fetchInitialData
ends up in my service.js config file , how that could be possible to instantiate store in my service.js file? how could it possible?– a_m_dev
Nov 12 at 12:45
first off, thank you for your respond, but how can i configure my store that it could be available for server side at the time that server needs to dispatch an action to fetch data from an endPoin??? that
fetchInitialData
ends up in my service.js config file , how that could be possible to instantiate store in my service.js file? how could it possible?– a_m_dev
Nov 12 at 12:45
you are a masterpiece , take care of your self , and many many many thanks for answering me the god damn right answer. love you...
– a_m_dev
Nov 12 at 20:25
you are a masterpiece , take care of your self , and many many many thanks for answering me the god damn right answer. love you...
– a_m_dev
Nov 12 at 20:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255959%2fdispatch-is-not-a-function-in-server-side-rendering%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why would it be a function in the first place? You are running
fetchInitialData
even before you configure the redux-store...dispatch
is a property of the redux-store, if you don't have a redux-store, then you can'tdispatch
... Also, notice thatactiveRoute.fetchInitialData
is not a thunk... If it was a thunk it would be dispatched.– Josep
Nov 12 at 10:52