【发布时间】:2021-07-30 17:14:54
【问题描述】:
我正在重构我的动作创建器并遇到了错误。基本上,这个动作是发送一个“get”请求,它应该检索一条消息和一个状态码(现在我至少尝试获取消息)。在浏览器中,当我打开网络选项卡时,请求已发送,但是我的控制台日志返回未定义。谁能帮我解决这个问题。 这是动作创建者本身:
export const handleFetchData =
(accessToken: string, refreshToken: string) => async (dispatch: Function) => {
try {
const response = await mainApiProtected.mePage({});
console.log(response);
} catch (error) {
console.log(error);
}
};
这里支持 TypeScript 文件:
import { AxiosRequestConfig } from "axios";
import HttpClientProtected from "./HttpClientProtected";
class MainProtected extends HttpClientProtected {
private static instanceCached: MainProtected;
private constructor() {
super("http://142.93.134.108:1111/");
}
static getInstance = () => {
if (!MainProtected.instanceCached) {
MainProtected.instanceCached = new MainProtected();
}
return MainProtected.instanceCached;
};
public mePage = (body: AxiosRequestConfig) => {
this.instance.get<{ message: string }>("/me", body);
};
}
export default MainProtected;
import { AxiosRequestConfig, AxiosResponse } from "axios";
import HttpClient from "./HttpClient";
abstract class HttpClientProtected extends HttpClient {
constructor(baseURL: string) {
super(baseURL);
this.initializeRequestInterceptors();
}
private initializeRequestInterceptors = () => {
this.instance.interceptors.request.use(this.handleMe);
this.instance.interceptors.response.use(this.handleMeResponse);
};
private handleMe = (config: AxiosRequestConfig) => {
const accessToken = localStorage.getItem("accessToken");
const updatedConfig = config;
updatedConfig.headers.Authorization = `Bearer ${accessToken}`;
return updatedConfig;
};
private handleMeResponse = ({ data }: AxiosResponse) => data;
}
export default HttpClientProtected;
【问题讨论】:
标签: typescript redux axios