Universal redux:browserhistory错误:无法读取未定义

时间:2016-10-09 13:37:57

标签: javascript node.js reactjs redux react-router-redux

我正在尝试使用express.js服务器为react-redux应用程序实现通用渲染,我正在遵循以下教程:http://redux.js.org/docs/recipes/ServerRendering.html

每当我尝试将rootreducer或App文件导入express server.js文件时,如教程中所述,我会收到以下错误:

\node_modules\react-router-redux\lib\sync.js:105
 unsubscribeFromHistory = history.listen(handleLocationChange);
                              ^
TypeError: Cannot read property 'listen' of undefined
at syncHistoryWithStore (C:\Users\user pc\Desktop\citydrill\node_modules\react-router-redux\lib\sync.js:105:35)

这是我的商店文件相关代码:

import { syncHistoryWithStore } from 'react-router-redux';
import { browserHistory } from 'react-router';

const store = createStore(rootReducer, initialState, compose(
    applyMiddleware(thunk)
));

export const history = syncHistoryWithStore(browserHistory, store);

然后我只是在clientApp中使用历史记录const,如下所示:

const router = (
<Provider store={store}>
    <Router history={history}>
        <Route name="Home" path="/" component={App}>
            <IndexRoute name="Home" component={Landing}></IndexRoute>
        </Route>
    </Router>
</Provider>

我在redux.js.org中尝试了很多不同的教程,而且我总是坚持这一点,因为我总是必须从我的express server.js文件中的客户端代码中导入一些文件,每当我这样做,我总是得到同样的错误。

如何实施通用redux?我究竟做错了什么?我跟着这么多教程,但没有人提到这个错误,为这些家伙怎么样但不在我的应用程序中呢?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您无法在服务器端使用$('button[name="deleteButton"]').click(function() { var row = $(this).closest('tr'); editTableDelete(row); }); ,因为名称表示它适用于浏览器。请改用function editTableDelete(row) { 'use strict'; var data = row.find('input').data(); data.content = row.find('#contentWrapper').text(); data.session = $.cookie('PHPSESSID'); data.type = 'delete'; if (confirm('Den Eintrag ' + data.content + ' wirklich löschen?')) { $.ajax({ url: '/adminControl/' + data.script, type: 'POST', data: data, dataType: "json", success: function (result) { // fade out loading animation $("td[contenteditable=true]").find("#updateLoader").fadeOut(400); if (result.success) { $(row).fadeOut(400, function() { $(row).remove(); }); $('.material_alert').html(result.message); $(this).animatedAlert({ containerAlert: $("#success_alert"), axisX: "right", axisY: "bottom", autoClose: "true", delayClose: "1000" }); } else { $('.material_alert').html(result.message); $(this).animatedAlert({ containerAlert: $("#error_alert"), axisX: "right", axisY: "bottom", autoClose: "true", delayClose: "1000", colorOverlay: $(this).attr("data-color-overlay") }); } } }); } }

https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md