【问题标题】:React: cannot retrieve data from a server (dispatch)React:无法从服务器检索数据(调度)
【发布时间】:2018-03-27 07:16:48
【问题描述】:

这是我第一次使用 React,当我尝试获取一些数据时遇到了一些问题。我的问题在getChargesByUser() 方法内部,我刚刚写了两个console.logs,第一个显示但第二个没有,这意味着我进入了方法但没有进入return (dispatch) => {...} 奇怪的是这不是我使用 return (dispatch) => {...} (如 loadUserList())的唯一方法,也是唯一我无法访问且不知道为什么的方法

const SHOW_PAYMENTS = 'SHOW_PAYMENTS';

const initialState = { 
  open_drawer: false,
  open_search: false,
  list_users: [],
  list_selectusers: [],
  countries: [],
  states: [],
  list_payments: [],
  user: [],
  useremail: [],
  save: [],
  eventtype: '',
  user_redirect: '',
  saveloading: false
};

export default function reducer(state = initialState, action) {
  switch (action.type) {
    case SHOW_PAYMENTS:
      return Object.assign({}, state, {list_payments: action.payload, eventtype: 'payments'});
    default:
      return state;
  }
}

export function loadUserList(page, perpage) {
  /* I HAVE ACCESS HERE */
  return (dispatch) => { 
    /* I ALSO HAVE ACCESS HERE */
    axios.get(`${config.host}/v1/user?page=${page}&perpage=${perpage}`, {
      headers: {'x-asd-apikey': config.apikey}
    }).then(response => {
      dispatch({type: LOAD_USERLIST, payload: response.data.data});
    }).catch(error => {
      dispatch({type: LOAD_USERLIST_ERROR, payload: error.response.data});
    });
  };
}

export function getChargesByUser(userid) {
  console.log('yes'); //<-- I have access here
  return (dispatch) => {
    console.log('nope'); // I have not accesss here
    axios.get(`http://localhost:7770/v1/payments/${userid}/resume`, {
      headers: {'x-asd-apikey': config.apikey}
    }).then(response => {
      console.log('response: ', response.data.data);
      dispatch({type: SHOW_PAYMENTS, payload: response.data.data.records});
    }).catch(error => {
      console.log('error: ', error.response.data);
      dispatch({type: SHOW_PAYMENTS, payload: { options: error.response.data}});
    });
  };
}

这就是我调用方法的地方

@connect(
  state => ({
    eventtype: state.users.eventtype,
    list_payments: state.users.list_payments,
  }, usersActions)
)

static propTypes = {
  eventtype: PropTypes.any,
  list_payments: PropTypes.any,
  getChargesByUser: PropTypes.func.isRequired,
  params: PropTypes.object
}

componentDidMount() {
  console.log('params: ', this.props.params.userid);
  this.props.getChargesByUser(this.props.params.userid);
}

【问题讨论】:

    标签: reactjs redux react-redux requestdispatcher


    【解决方案1】:

    当在 Promise 内部时,您需要返回一个类似 Promise 的对象来继续链。

    所以:如果你想进入 then/catch after(axios 调用返回 Promise),你需要 return axios.get(...

    【讨论】:

    • 抱歉,我不想返回 axios 承诺中的值,我需要发送该承诺中的信息 dispatch({type: SHOW_PAYMENTS, payload: response.data.data.records});
    • 是的,但是要到达 .then 内的这行代码,您需要返回 axios 承诺。
    • 我刚刚回答了我自己的问题,但我很感谢你的回答和你的时间,卢卡。谢谢和问候。 :)
    【解决方案2】:

    抱歉,我整天都在处理这个问题,并没有注意到问题出在我的代码所在的@connect()

    @connect(
      state => ({
        eventtype: state.users.eventtype,
        list_payments: state.users.list_payments,
      }, usersActions)
    )
    

    它应该是这样的:

    @connect(
      state => ({
        eventtype: state.users.eventtype,
        list_payments: state.users.list_payments
      }),
      usersActions)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 2016-05-04
      • 2019-12-05
      • 1970-01-01
      相关资源
      最近更新 更多