Node.js 无法使用redux从api获取用户

Node.js 无法使用redux从api获取用户,node.js,reactjs,express,redux,react-redux,Node.js,Reactjs,Express,Redux,React Redux,使用redux从api获取数据时遇到问题。我正在使用React前端和express后端。我试图在后端通过GoogleOAuth设置身份验证,然后将标题(设置成功)发送回我的React客户端。我可以从api成功地获取一些数据,但无法使用redux获取用户 获取用户的我的操作创建者: export const fetchUser = () => async dispatch => { const res = await posts.get ('/api/current_user');

使用redux从api获取数据时遇到问题。我正在使用React前端和express后端。我试图在后端通过GoogleOAuth设置身份验证,然后将标题(设置成功)发送回我的React客户端。我可以从api成功地获取一些数据,但无法使用redux获取用户

获取用户的我的操作创建者:

export const fetchUser = () => async dispatch => {
  const res = await posts.get ('/api/current_user');
  dispatch ({type: FETCH_USER, payload: res.data});
};
在上面的代码中,posts只是一个导出,它允许我使用axios向api发出请求

将用户数据发送到客户端的路由处理程序:

  app.get ('/api/current_user', (req, res) => {
    console.log("this is a req user", req.user)
    res.send (req.user);
  });
请注意,路由处理程序上的console.log成功地记录了我尝试获取的用户

在客户端,我认为我已将redux存储正确连接到该组件:

  render () {
  console.log(this.props)
    return (
      <div>
        <div>

          {/* {this.renderAuthButton ()} */}
        </div>

      </div>
    );
  }
}

function mapStateToProps(state) {
  return {auth: state.auth};
}

export default connect (mapStateToProps)(GoogleAuth);
render(){
console.log(this.props)
返回(
{/*{this.renderAuthButton()}*/}
);
}
}
函数MapStateTops(状态){
返回{auth:state.auth};
}
导出默认连接(MapStateTops)(GoogleAuth);
当我尝试记录this.props时,它根本不会记录用户。我尝试在render方法中调用fetchUser函数,但它也没有返回用户。
任何帮助都将不胜感激,我很乐意提供任何其他信息。提前谢谢你

尝试将dispatch连接到redux,如下所示:

import { fetchUser } from '....'
//
//
//
function mapStateToProps(state) {
  return {auth: state.auth};
}

function mapDispatchToProps(dispatch) {
  return {fetchUser: () => fetchUser(dispatch)};
}

export default connect (mapStateToProps, mapDispatchToProps)(GoogleAuth);
组件生命周期中的调用操作:

componentDidMount() {
   this.props.fetchUser()
}
render () {
   console.log(this.props) // Here you will see data that return from 
   // fetchUser action in store redux at the second time of render Component
   return (
       ...
   )
}

使用redux thunk映射动作创建者的正确方法应该是:

从“..”导入{fetchUser}
// ...
函数MapStateTops(状态){
返回{auth:state.auth};
}
功能图DispatchToprops(调度){
返回{fetchUser:()=>dispatch(fetchUser())};
}
导出默认连接(mapStateToProps、mapDispatchToProps)(GoogleAuth);
或者只需将动作创建者作为映射对象发送到connect的第二个参数:

从“..”导入{fetchUser}
// ...
函数MapStateTops(状态){
返回{auth:state.auth};
}
导出默认连接(mapstatetops,{fetchUser})(GoogleAuth);

阅读更多信息。

您是否也使用
mapDispatchToProps
将您的动作创建者(您的
fetchUser
)映射到您的组件?谢谢您的回复!我试过这样做,但我发现分派不是一个函数。可能是因为我在动作创建者中使用了redux thunk和async函数。我尝试在组件的生命周期中调用fetchUser,但只有一个console.log。我相信如果一切正常,我会看到2个日志,因为一旦async fetchUser函数获取数据,存储应该更新并重新呈现组件?我还在fetch_user reducer中添加了一个console.log,它似乎得到了正确的action.type,但仍然没有数据我尝试过这样做,但我得到的调度不是一个函数错误。你知道我为什么会犯这个错误吗?我还尝试将我的action creator作为映射对象发送到connect的第二个参数,并在组件的生命周期中调用它,该组件工作正常,但仍然无法正确获取用户,它只返回一个空字符串。当我直接转到api地址时,用户正确地显示为一个对象。您从哪里得到“dispatch is not a function error”(分派不是函数错误)?在地图发送到道具中还是在你的动作创建者中?您是否已使用react redux Provider包装您的应用程序?尝试发布存储初始化的代码,以检查是否正确应用了redux thunk。我现在看到了错误,我只是从另一个答案复制了代码示例,mapDispatchToProps应该返回
{fetchUser:()=>dispatch(fetchUser())}
而不是
{fetchUser:dispatch(fetchUser())}。我在回答中修正了它。