2016-05-18 29 views
1

當我第一次運行服務器時node app.js一切都很好我得到listening on 5050。然後我去localhost:5050 /和我在控制檯中得到這些警告服務器正在運行。TypeError:_this.store.getState不是使用從Redux連接時的功能

Warning: Failed propType: Required prop `store.subscribe` was not specified in `Provider`. 
Warning: Failed Context Types: Required child context `store.subscribe` was not specified in `Provider`. 

組件呈現精細那裏,但是當我去到本地主機:5050/ballerviews我得到這個服務器上

TypeError: _this.store.getState is not a function 
    at new Connect (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react-redux/lib/components/connect.js:133:38) 
    at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactCompositeComponent.js:148:18) 
    at [object Object].wrapper [as mountComponent] (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactPerf.js:66:21) 
    at Object.ReactReconciler.mountComponent (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactReconciler.js:37:35) 
    at ReactDOMComponent.ReactMultiChild.Mixin.mountChildren (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactMultiChild.js:241:44) 
    at ReactDOMComponent.Mixin._createContentMarkup (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactDOMComponent.js:591:32) 
    at ReactDOMComponent.Mixin.mountComponent (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactDOMComponent.js:479:29) 
    at Object.ReactReconciler.mountComponent (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactReconciler.js:37:35) 
    at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactCompositeComponent.js:225:34) 
    at [object Object].wrapper [as mountComponent] (/Users/freddiecabrera/Desktop/ullibol-client/node_modules/react/lib/ReactPerf.js:66:21) 

您可以在我的Github查看完整的項目,因爲我覺得通過查看它有你可以得到最好的理解。

我一直試圖弄清楚今天整天,我不能。在我嘗試在服務器上呈現之前,它工作正常。我很感激幫助,如果您需要更多信息來回答問題,請讓我知道。

回答

5

只是有兩個小問題:

1 ES2015模塊 - > CommonJS的

您在src/store.js使用ES2015 export default,但是你再在app.js通過CommonJS的需要它line #11

這當然沒有問題,但您需要時可以訪問默認屬性。更新行,如下所示:

// Old 
const store = require('./src/store.js') 

// New 
const store = require('./src/store.js').default 

2.服務器

上調用documentsrc/components/BallerViews.jsxline #41您正在訪問的文件,這將不會存在於服務器上。一個快速解決方法是將該行包含在條件中:

// Old 
document.body.style.backgroundColor = '#0e0e13' 

// New 
if (typeof document !== 'undefined') { 
    document.body.style.backgroundColor = '#0e0e13' 
} 
+0

該死!非常感謝你,你救了我,我真的很感謝你的幫助。所以基本上任何時候我想訪問文檔或窗口,我必須將其包裝在條件? –

+0

@FreddieCabrera是的,但只有當你服務器渲染。節點不瞭解DOM。一種流行的做法是像'__CLIENT__'和'__SERVER__'這樣的全局變量,以便您可以輕鬆地定位每個變量。您可以在服務器入口點(app.js)和webpack配置中執行此操作。我已經創建了一個要點,告訴你如何設置它:https://gist.github.com/iamlacroix/38fb4ce4670313bae2a913bbc54b2ae3 –

+0

@MichaelLaCroix。默認解決這個對我來說也是如此。由於許多示例將ES2015與傳統相結合,因此很難遵循最佳做法。 – jjr2527

相關問題