dispatch is not a function in Server Side Rendering










1














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 ????










share|improve this question























  • 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
















1














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 ????










share|improve this question























  • 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














1












1








1







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 ????










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















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













1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer




















  • 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










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



);













draft saved

draft discarded


















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









2














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.






share|improve this answer




















  • 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















2














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.






share|improve this answer




















  • 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













2












2








2






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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??? 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
















  • 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















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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto