redux-saga call之后数据返回为undefined
// import { delay } from "redux-saga";
import { call, put, takeEvery } from "redux-saga/effects";
import { todoTypes } from "../actions/todoAction";
import Apis from "../apis";
function* fetchData(actions) {
try {
const data = yield call(Apis.getCategory, "");
console.log("data=", data);
yield put({ type: todoTypes.TEST_SAGA_SUCCESS });
} catch (ex) {
console.log("error=", ex);
}
}
export default function* watchTodo() {
yield takeEvery(todoTypes.TEST, fetchData);
}
import Request from "../../utils/request";
class Apis {
getCategory = () => Request.get("/users");
}
export default new Apis();
import axios from "axios";
import config from "../config/base.config";
/**
* 创建axios实例
*/
const instance = axios.create({
baseURL: config.host
});
/**
* axios 拦截器
* 拦截并设置token
*/
instance.interceptors.request.use(
config => {
//todo:token 没有token或者token过期都要跳转到登录页面
config.headers.Authorization = ``;
return config;
},
error => {
return Promise.reject(error);
}
);
/**
* Http服务类
* get
* post
* delete
* put
* patch
*/
export default class Request {
static get(url, params = {}) {
return new Promise((resolve, reject) => {
instance
.get(url, { params: { ...params } })
.then(({ data }) => {
resolve(data.data);
})
.catch(err => {
reject({ err: JSON.stringify(err) });
});
});
}
static delete(url, params = {}) {
return new Promise((resolve, reject) => {
instance
.delete(url, { params: { ...params } })
.then(({ data }) => {
resolve(data.data);
})
.catch(err => {
reject({ err: JSON.stringify(err) });
});
});
}
static post(url, params = {}) {
return new Promise((resolve, reject) => {
instance
.post(url, { ...params })
.then(({ data }) => {
resolve(data);
})
.catch(err => {
reject({ err: JSON.stringify(err) });
});
});
}
static put(url, params = {}) {
return new Promise((resolve, reject) => {
instance
.post(url, { ...params })
.then(({ data }) => {
resolve(data.data);
})
.catch(err => {
reject({ err: JSON.stringify(err) });
});
});
}
static patch(url, params = {}) {
return new Promise((resolve, reject) => {
instance
.patch(url, { ...params })
.then(({ data }) => {
resolve(data.data);
})
.catch(err => {
reject({ err: JSON.stringify(err) });
});
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了,是因为 我get 方法返回的时候应该是resolve(data),我写成了resolve(data.data),去掉这个后面的.data就成功了