用promise响应redux问题,因为我的变量为null

时间:2019-03-14 13:55:44

标签: reactjs redux

我有一个React-Redux应用程序,但是诺言有问题。首先,我想给你我的代码:

问题是我的dateApi第二次只是“令人惊讶”。我需要做出承诺,但我不知道该怎么做。

问题:这是我在前端反应中的方法:

...
    this.props.handleLastWeek(this.props.dateFrom, this.props.dateTo);
    // ^^ this returns dataApi ^^

    var i = 1;
    var j = 1;
    var k = 1;
    var l = 0;
    var dateArray = [];
    this.props.dataApi.map(dA => {
    ... // this.props.dataApi is null

减速器:

case GET_DATA_LAST_WEEK:
  return state.set("dataApi", []).set("isLoading", true);

case GET_DATA_LAST_WEEK_SUCCESS:
  return state.set("isLoading", false).set("dataApi", action.response);

这是我的操作:(响应是我的dataApi)

export function getDataLastWeekAction(from, to) {
  return {
    type: GET_DATA_LAST_WEEK,
    from,
    to
  };
}

export function successDataLastWeekAction(response) {
  return {
    type: GET_DATA_LAST_WEEK_SUCCESS,
    response
  };
}

这是我的redux-saga函数:

    export function* handleLastWeek() {
  const from = yield select(makeSelectDateFrom());
  const to = yield select(makeSelectDateTo());
  const requestURL = `/api/daydate`;

  try {
    const response = yield call(request, requestURL, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${getToken()}`,
        Accept: "application/json",
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        from,
        to
      })
    });

    if (response) {
      yield put(successDataLastWeekAction(response));
    } else {
      yield put(errorDataLastWeekAction("Spróbuj ponownie za moment."));
    }
  } catch (err) {
    yield put(errorDataLastWeekAction(err));
  }
}

0 个答案:

没有答案