Axios呼叫未返回承诺

时间:2018-10-21 14:33:38

标签: javascript reactjs promise axios

以下axios调用似乎很好,但是它永远不会返回promise,但会返回undefined

export function getAllSubmissionDetails() {
  return (dispatch) => {
    axios
      .get(`/rbac/members/filledForm/getMain/show/1/data/all`, {
        headers: {
          'Content-Type': 'application/json',
          'Pragma': 'no-cache'
        }
      })
      .then((memResponse) => {
        console.log('%%%%%%%');
        console.log(memResponse);
        if (memResponse.data.status == 200) {
          var mem_list = memResponse.data.msg;
          if (mem_list.length > 0) {
            mem_list = _.sortBy(mem_list, "submissionid").reverse()
            dispatch(memIdeaListSuccess(mem_list, null));
            return memResponse
          } else {
            dispatch(memIdeaListFailure("No Submissions", null));
            return memResponse
          }
        } else {
          dispatch(memIdeaListFailure(memResponse.data.msg, null));
          return memResponse
        }

      })
      .catch((err) => {
        dispatch(serviceError(err));
        return Promise.reject(error)
      });
  };
}

1 个答案:

答案 0 :(得分:3)

您不会返回axios返回的值,因此不会返回未定义的值

export function getAllSubmissionDetails(){
                return (dispatch) => {
                        // return value here
                        return axios.get(`/rbac/members/filledForm/getMain/show/1/data/all`, {
                        headers: { 'Content-Type': 'application/json','Pragma': 'no-cache' }
                      })
                        .then((memResponse) => {
                            console.log('%%%%%%%');
                            console.log(memResponse);
                            if(memResponse.data.status == 200){
                                var mem_list = memResponse.data.msg;
                                if(mem_list.length>0) {
                                    mem_list = _.sortBy(mem_list,"submissionid").reverse()
                                    dispatch(memIdeaListSuccess(mem_list,null));
                                    return memResponse
                                } else {
                                    dispatch(memIdeaListFailure("No Submissions",null));
                                    return memResponse 
                                }
                            }else{
                                dispatch(memIdeaListFailure(memResponse.data.msg,null));
                                return memResponse
                            }

                        })
                        .catch((err) => {dispatch(serviceError(err)); return Promise.reject(error)});
                    };
              }