我正在使用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;
答案 0 :(得分:0)
基本上我找到了答案,我忘了在我的功能中回复我的承诺
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;