2017-08-01 13 views
0

私の電極アプリの初期状態を別のAPIへのhttp呼び出しで設定しようとしています。私は、http要求を作成し、要求にエラーのないデータがあるときに解決する約束を返すgetLatestProducts関数を書きました。私はcreateReduxStore関数から約束を返さなければならないことを知っていますが、このエラーを消すための約束をどこで解決すべきか分かりません。ReduxRouterEngineエラー:TypeError:未定義の 'then'プロパティを読み取ることができません

Electrode ReduxRouterEngine Error: TypeError: Cannot read property 'then' of undefined 
    at ReduxRouterEngine._handleRender (C:\reactjs\unitest\node_modules\electrode-redux-router-engine\lib\redux-router-engine.js:119:7) 
    at _matchRoute.then (C:\reactjs\unitest\node_modules\electrode-redux-router-engine\lib\redux-router-engine.js:85:21) 
    at bound (domain.js:280:14) 
    at runBound (domain.js:293:12) 
    at runCallback (timers.js:637:20) 
    at tryOnImmediate (timers.js:610:5) 
    at processImmediate [as _immediateCallback] (timers.js:582:5) 
From previous event: 
    at ReduxRouterEngine.render (C:\reactjs\unitest\node_modules\electrode-redux-router-engine\lib\redux-router-engine.js:62:8) 
    at module.exports.req (C:/reactjs/unitest/src/server/views/index-view.jsx:76:27) 
    at callUserContent (C:\reactjs\unitest\node_modules\electrode-react-webapp\lib\react-webapp.js:88:17) 
    at renderSSRContent (C:\reactjs\unitest\node_modules\electrode-react-webapp\lib\react-webapp.js:171:11) 
    at options (C:\reactjs\unitest\node_modules\electrode-react-webapp\lib\react-webapp.js:176:12) 
    at handler (C:\reactjs\unitest\node_modules\electrode-react-webapp\lib\hapi\index.js:43:13) 
    at Object.internals.handler (C:\reactjs\unitest\node_modules\hapi\lib\handler.js:101:51) 
    at request._protect.run (C:\reactjs\unitest\node_modules\hapi\lib\handler.js:32:23) 
    at internals.Protect.run (C:\reactjs\unitest\node_modules\hapi\lib\protect.js:59:12) 
    at exports.execute (C:\reactjs\unitest\node_modules\hapi\lib\handler.js:26:22) 
    at each (C:\reactjs\unitest\node_modules\hapi\lib\request.js:404:16) 
    at iterate (C:\reactjs\unitest\node_modules\items\lib\index.js:36:13) 
    at done (C:\reactjs\unitest\node_modules\items\lib\index.js:28:25) 
    at internals.Auth._authenticate (C:\reactjs\unitest\node_modules\hapi\lib\auth.js:222:16) 
    at internals.Auth.authenticate (C:\reactjs\unitest\node_modules\hapi\lib\auth.js:197:17) 
    at each (C:\reactjs\unitest\node_modules\hapi\lib\request.js:404:16) 
    at iterate (C:\reactjs\unitest\node_modules\items\lib\index.js:36:13) 
    at done (C:\reactjs\unitest\node_modules\items\lib\index.js:28:25) 
    at internals.state (C:\reactjs\unitest\node_modules\hapi\lib\route.js:362:16) 

マイコード:

import ReduxRouterEngine from "electrode-redux-router-engine"; 
 
import {routes} from "../../client/routes"; 
 
import {createStore} from "redux"; 
 
import rootReducer from "../../client/reducers"; 
 
import util from "util"; 
 
import http from "http"; 
 

 
const Promise = require("bluebird"); 
 
const getLatestProducts = function(count){ 
 
\t const options = { 
 
\t \t host: "127.0.0.1", 
 
\t \t port: 4000, 
 
\t \t path: '/products/latest/'+count, 
 
\t \t method:'GET', 
 
\t \t header:{ 
 
\t \t \t 'Content-Type':'application/json' 
 
\t \t } 
 
\t }; 
 
\t return new Promise((resolve, reject) => { 
 
\t \t http.get(options, (response) => { 
 
\t \t \t var body = ""; 
 
\t \t \t var errors = ""; 
 
\t \t \t response.on("data", function(chunk){ 
 
\t \t \t \t body += chunk; 
 
\t \t \t }); 
 
\t \t \t response.on("errors", function(error){ 
 
\t \t \t \t errors = error.message; 
 
\t \t \t \t return reject(errors); 
 
\t \t \t }); 
 
\t \t \t response.on("end", function(){ 
 
\t \t \t \t return resolve(body); 
 
\t \t \t }); 
 
\t \t }) 
 
\t }); 
 
} 
 
    function createReduxStore(req, match) { // eslint-disable-line 
 
let initialState; 
 
\t getLatestProducts("12") 
 
\t .then((result) => { 
 
\t \t initialState = { 
 
\t \t \t latestProducts: {products:result, error:null, loading:false} 
 
\t \t }; 
 
\t \t return result; 
 
\t }) 
 
\t .then(() => { 
 
\t \t const store = createStore(rootReducer, initialState); 
 
\t \t return Promise.resolve(store); \t 
 
\t }) 
 
\t .catch((error) => { 
 
\t \t console.log(error); 
 
\t \t return Promise.reject(error); 
 
\t }) 
 
\t 
 
} 
 

 
// 
 
// This function is exported as the content for the webapp plugin. 
 
// 
 
// See config/default.json under plugins.webapp on specifying the content. 
 
// 
 
// When the Web server hits the routes handler installed by the webapp plugin, it 
 
// will call this function to retrieve the content for SSR if it's enabled. 
 
// 
 
// 
 

 
module.exports = (req) => { 
 
    const app = req.server && req.server.app || req.app; 
 
    if (!app.routesEngine) { 
 
    app.routesEngine = new ReduxRouterEngine({routes, createReduxStore}); 
 
    } 
 

 
    return app.routesEngine.render(req); 
 
};

答えて

0

が、私は問題を解決することができます。私は新しいPromiseインスタンスを返し、他の関数の結果に基づいて解決/拒否する必要があります。したがって、createReduxStoreのコードは次のようになります。

function createReduxStore(req, match) { // eslint-disable-line 
    let initialState; 
    return new Promise((resolve, reject) => { 
     getLatestProducts("12") 
     .then((result) => { 
      initialState = { 
       latestProducts: {products:result, error:null, loading:false} 
      }; 
      const store = createStore(rootReducer, initialState); 
      resolve(store); 
     }) 
    }) 
    .catch((error) => { 
     console.log(error); 
     reject(error); 
    }) 

} 
関連する問題