我正在尝试使用redux工具包来执行api调用,但在调度时遇到了这个错误
无法读取未定义的的财产(读取“订阅”)
索引ts
import api from "./api"; export const { useFetchAllMaterialsQuery } = api;
每平方英尺
import { createApi } from "@reduxjs/toolkit/query/react"; import type { OperationsEntity } from "models"; import { fetchAxiosBaseQuery, getCookie } from "services/httpService"; // Create api endpoints export default createApi({ reducerPath: "fetchAllMaterialsReportApi", baseQuery: fetchAxiosBaseQuery({ baseUrl: getCookie("apiUrl") }), endpoints: (builder) => ({ /** * Fetch list of operations that have material(s). */ fetchAllMaterials: builder.query<OperationsEntity[], string | number>({ query: (id) => ({ url: `/v2/projects/${id}/mydata`, }), }), }), });
选择器.ts
import { useSelectedProjectId } from "hooks"; import { useAppSelector } from "redux/store"; import api from "./api"; /** * @returns Safran pending count. */ export function useAllMaterialOperations() { const projectId = useSelectedProjectId(); return useAppSelector( (state: any) => api.endpoints.fetchAllMaterials.select(projectId!)(state).data || [] ); } /** * @returns true if the fetch materials are in loading status (fetching), false otherwise. */ export function useAllMaterialOperationsLoading() { const projectId = useSelectedProjectId(); return useAppSelector( (state: any) => { return api.endpoints.fetchAllMaterials.select(projectId!)(state).isLoading; } ); } /** * @returns the data and the loading status of the fetch materials call. */ export function useFetchAllMaterialsWithLoadingStatus() { const data = useAllMaterialOperations(); const isLoading = useAllMaterialOperationsLoading(); return { data, isLoading, }; }
fetch查询导致应用程序崩溃,并被调用如下:
const MyComp = ({ data }) => { const id = useId(); const materialDataQuery = useFetchAllMaterialsQuery(id); }
代码中是否存在导致此错误的任何明显错误?