[英]Undefined response from axios action creator
我正在重构我的动作创建者并遇到了错误。 基本上,这个动作是发送一个“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;
看起来您的mePage
实际上并没有返回 Promise 或任何东西:
public mePage = (body: AxiosRequestConfig) => {
this.instance.get<{ message: string }>("/me", body);
};
您可能打算在那里添加一个return
,或者删除{ }
括号以获得隐式返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.