TypeError:无法读取属性'然后'在进行异步调用时未定义

时间:2017-03-28 23:33:06

标签: reactjs react-redux redux-thunk react-router-redux

我一直在尝试处理服务器端渲染的应用程序。但我一直被困在我收到错误的地方'无法阅读财产'然后'未定义的'。一旦我的请求进入服务器渲染页面,我需要从数据库加载初始状态并将其存入store.But当我尝试从服务器端获取数据时,我得到上述错误。请找到我的服务器端代码以呈现初始页面

if (renderProps) {

  const components = renderProps.components;

  var componentObj = renderProps.components[2].WrappedComponent;
  const fetchData = (componentObj && componentObj.fetchDatas) || (() => Promise.resolve());
  const { location, params, history } = renderProps;
  const store = createStore(reducers, initialState, applyMiddleware(thunk));
  // calling this function which is place inside another component
  // I get error at below line stating cannot read property .then of undefined
   fetchData({ store, location, params, history })
  .then(() => {
    const markup= renderToString(
      <Provider store={store}>
        <RouterContext {...renderProps} />
      </Provider>
    )

    const state = store.getState();
    return res.render('index', { markup,initialState: 
    JSON.stringify(store.getState()) });
    // if the current route matched we have renderProps
   }).catch((err)=> {
    console.log("error",err);
   })
  } 

请找到我定义了名为

的函数的代码
class IndexPage extends React.Component {
  componentDidMount () {
    this.props.getCoffeeOrders();
  }
  // call the action creator
  static fetchDatas({store}) {
     store.dispatch(getCoffeeOrders);
  }
  render() {
    // render code goes here
}

}

另请找到我的动作创建者,它在动作创建者中进行异步调用以从服务器获取数据

import axios from 'axios';
export const FETCH_COFFEE_ORDERS = 'FETCH_COFFEE_ORDERS';
export  function getCoffeeOrders() {
//below line is getting printed on console
console.log("getting coffee orders inside");
return ((dispatch,getState) => {
    // below line is not getting printed
    // I think control does not come here 
    console.log("mKe a call");
     return axios.get('http://localhost:8080/api/fetchcoffeeData').then((response)=> {
        console.log("got response", response);
        dispatch({
            type: FETCH_COFFEE_ORDERS,
            data
        });

    })
    .catch((err) => {
        console.log("got error while fetching data",err);
    });
});
}

控件没有出现在我在另一个函数中的getCoffeeOrders中打印的行。 任何帮助,将不胜感激。提前致谢

1 个答案:

答案 0 :(得分:0)

你没有在fetchDatas静态函数中返回promise它应该是这样的:

pck.Load(File.OpenRead(Server.MapPath(@"~\EXCELS\Book2.xlsx")));

但是我仍然不确定redux dispatch是否会返回任何内容来查看this github issue