未定义数据,但我们正在控制台中记录数据



从后端提取数据后,数据将返回undefine。当我console.log时,数据会在控制台中返回。但当我尝试使用数据时,它返回了未定义的


//Here is the my logic using axios but it didnot work, it keeps throwing 404 error and undefined data
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import axios from "axios";
const MyBaseQuery =
({ baseUrl } = { baseUrl: "http://localhost:5000" }) =>
async ({ url, method, data }) => {
try {
const result = await axios({ url: baseUrl + url, method, data });
return { data: result.data };
} catch (axiosError) {
let err = axiosError;
return {
error: { status: err.response?.status, data: err.response?.data },
};
}
};
export const getAllCarsApi = createApi({
reducerPath: "getAllCarsApi",
baseQuery: MyBaseQuery,
endpoints(build) {
return {
getAllCarsApi: build.query({
query: () => ({ url: "all-cars", method: "GET" }),
}),
};
},
});
export const { useGetAllCarsQuery } = getAllCarsApi;    

我通过在函数中添加异步和等待来解决这个问题。

最新更新