typescript - 来自 axios 操作创建者的未定义响应
问题描述
我正在重构我的动作创建者并遇到了一个错误。基本上,这个动作是发送一个“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
,或删除{ }
括号以进行隐式返回。
推荐阅读
- sql - PostgreSQL:获取所有包含大写字母的行
- python - 如何在python的for循环中打印输出之前设置标题?
- c - STM32L4R5 的 SystemClock 配置问题
- javascript - 在 Shadow DOM 中的 setTimeout 中的 event.target 发生了变化?
- go - 如何在无限 for 循环中实现 goroutine;for{} 在等待组完成后重试?
- python - Django 'tuple' 对象没有属性 'save'
- sql - 查询以根据特定时间范围过滤掉数据
- javascript - 如何找到()一个对象属性匹配多个指定数组中的值
- python - MobilenetV3 Top 5 准确性问题
- python - 如何用多个单独的训练数据训练 LSTM 模型?