【问题标题】:React-Redux-Sagas - not returning response -not showing errorsReact-Redux-Sagas - 不返回响应 - 不显示错误
【发布时间】:2025-12-02 09:35:01
【问题描述】:

我正在使用 Sagas 开发 Redux 应用程序,当我调用操作创建者时,它会返回和操作,并且它确实通过 Sagas 调用 API,但是,我发现我的 Saga 函数内部没有响应。我不知道为什么会发生这种情况,我已经尝试了所有方法,如果有人有任何建议,我将不胜感激。谢谢

sagas.js

import { call, put } from 'redux-saga/effects';
import { takeEvery } from 'redux-saga/effects'
import { createMatrix } from './utils';
import { browserHistory } from 'react-router';

import { CREATE_MATRIX_REQUEST, CREATE_MATRIX_SUCCESS, CREATE_MATRIX_ERROR } from './constants';

export function* createMatrixSaga(action) {
   try {
      //Calls the API and sends payload
      const result = yield call(createMatrix, action.data);
      // We send an action that tells Redux we're sending a payload
      console.log(result)
      yield put({type: CREATE_MATRIX_SUCCESS, success: result});
      //Forward to /reports once actions is sent
      // yield call(forwardTo, '/reports');

   } catch(error) {
     // We send an action that tells Redux we're sending an error
     yield put({type: CREATE_MATRIX_ERROR, error: error });
   }
}


function* watchFetchData() {
  // We send an action that tells Redux we're sending a request
    yield takeEvery(CREATE_MATRIX_REQUEST, createMatrixSaga);
}


export default [
  watchFetchData,
];

// Little helper function to abstract going to different pages
export function* forwardTo(location) {
  yield call(browserHistory.push, location);
}

utils.js

   import axios from 'axios';
    import cookie from 'react-cookie';


    export function createMatrix({domain, kw}) {

      var url = '';
      var keywords = kw;
      var encoded = encodeURI(keywords);
      var data = {
         key: '',
         keywords: encoded,
         analysisname: domain,
         country:1,
         location:null,
         trafficstats:false,
         use_majestic_api:false
      }
      axios.post(url, data).then((response) => {
          return response.data
      })
      .catch((error) => {
        console.log(error, 'This a big error')
        throw error
      });
    }


    export default createMatrix;

【问题讨论】:

    标签: redux redux-saga


    【解决方案1】:

    基本上找到了答案,我忘了在我的函数中返回我的承诺

    import axios from 'axios';
    import cookie from 'react-cookie';
    
    
    export function createMatrix({domain, kw}) {
    
      var url = 'https://termexplorer.com/api/keyword-analyzer/new';
      var keywords = kw;
      var encoded = encodeURI(keywords);
      var data = {
         key: 'f3800f11c8266f16a8943981cdd01192',
         keywords: encoded,
         analysisname: domain,
         country:1,
         location:null,
         trafficstats:false,
         use_majestic_api:false
      }
      return axios.post(url, data).then((response) => {
          return response.data
      })
      .catch((error) => {
        console.log(error, 'This a big error')
        throw error
      });
    }
    
    
    export default createMatrix;
    

    【讨论】: