I am exploring node and express with redux where I want to set a cookie after the page has been rendered and wanna use the updated state for setting a cookie where I am getting this error.
Please help me in getting the answers of following ? 1) Let me know wheather the syntax I've written is correct or not and if not ,then what should be done? 2) How can i set a cookie to response after successful render of ejs file?
router.get('/dashboard',isLoggedIn,(req, res) => {
store.dispatch(initialize(reduxOauthConfig))
.then(() => match({ routes: dashroutes, location: req.url }, (error, redirectLocation, renderProps) => {
if (redirectLocation) {
res.redirect(301, redirectLocation.pathname + redirectLocation.search);
} else if (error) {
res.status(500).send(error.message);
} else if (!renderProps) {
res.status(404).send('Not found');
} else {
loadOnServer({ ...renderProps, store })
.then(() => {
const componentHTML = ReactDOMServer.renderToString(
<Provider store={store}>
<ReduxAsyncConnect {...renderProps}/>
</Provider>
);
const initialState = store.getState();
res.render('dashboard.ejs', {
markup: componentHTML,
intialState:initialState
});
})
.then(html => {
// !!! IMPORTANT TO PERSIST SESSION IF JavaScript failed to load / initialize
res.cookie('authHeaders', JSON.stringify(getHeaders(store.getState())), { maxAge: now() * 0.001 + 14 * 24 * 3600 });
res.end(html);
})
.catch(err => {
console.log(err.stack);
res.end(err.message);
});
}
}));
});