Home > Back-end >  when I try to destructing the data from react query I am getting Property 'data' does not
when I try to destructing the data from react query I am getting Property 'data' does not

Time:06-13

I am facing issue with react query when I try to destructing the data like this way const {data: getAllChannelSettingTypes} = useGetAllChannelSettingTypes(); I am gettin this error Property 'data' does not exist on type 'void'

Maybe there is a problem with returning the userQueries

const useGetAllChannelSettingTypes = () => {
  const {data: defaultSettingTypes} = useQuery(SETTING_TYPES, () =>
    Api.user.getSettingTypes(),
  );
  const userQueries = useQueries(
    defaultSettingTypes.map((type: any) => {
      return {
        queryKey: ['SETTING_VALUE', type.channelSettingTypeId],
        queryFn: () => Api.user.getSettingValues(type.channelSettingTypeId),
      };
    }),
  );
  console.log(userQueries);
  return userQueries;
};

CodePudding user response:

According to the documentation useQueries returns an array:

The useQueries hook returns an array with all the query results.

Each element in the array has these properties:

  • status: String
  • isIdle: boolean
  • isLoading: boolean
  • isSuccess: boolean
  • isError: boolean
  • isLoadingError: boolean
  • isRefetchError: boolean
  • data: TData, defaults to undefined.
  • dataUpdatedAt: number
  • error: null | TError
  • errorUpdatedAt: number
  • isStale: boolean
  • isPlaceholderData: boolean
  • isPreviousData: boolean
  • isFetched: boolean
  • isFetchedAfterMount: boolean
  • isFetching: boolean
  • isRefetching: boolean
  • failureCount: number
  • errorUpdateCount: number
  • refetch: (options: { throwOnError: boolean, cancelRefetch: boolean }) => Promise
  • remove: () => void

So try destructing the data in a loop like this:

for(let i = 0; i < userQueries.length; i  ){
  const {data: getAllChannelSettingTypes} = useGetAllChannelSettingTypes()
  // do something with that data of the given query
}

If you are only interested in the first result you could do it like this:

const {data: getAllChannelSettingTypes} = useGetAllChannelSettingTypes()[0]
   

CodePudding user response:

try removing the params "data"

  • Related