2017-08-01 144 views
0

我試圖通過http調用將另一個API設置爲我的電極應用程序的初始狀態。我編寫了getLatestProducts函數,該函數使http請求返回一個承諾,該請求解決請求中沒有錯誤的數據。我知道我必須從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); 
    }) 

} 
相關問題