Home > Net >  async argument in useQueries (getting undefined) – React Query
async argument in useQueries (getting undefined) – React Query

Time:11-10

I'm using a hook to fetch a user from firebase:

  const [user, isUserLoading, isUserError] = useDocument(
    doc(getFirestore(app), 'users', currentUserId)
  );

I'm interested in an array in the user object:

const currentUserSurveys = user && user?.data().surveys;

Now I want to pass currentUserSurveys which is an array of strings as an argument to this hook:

const clientSurveys = useClientSurveys(currentUserSurveys);

The hook is defined like this:

function useClientSurveys(surveysIds) {
  const { isAdmin } = useAdminStatus();

  const clientSurveysQueries = useQueries({
    queries: surveysIds?.map(surveyId => {
      return {
        queryKey: ['clientSurvey', surveyId],
        queryFn: () => getSurvey(surveyId),
        enabled: !!surveysIds && surveysIds.length > 0 && !isAdmin,
      };
    }),
  });
  return clientSurveysQueries;
}

I'm getting this error:

Cannot read properties of undefined (reading 'map')

I thought this line helps to only call the query when surveysIds is defined:

enabled: !!surveysIds && surveysIds.length > 0 && !isAdmin,

How do I solve this?

CodePudding user response:

do

(surveysIds || [])?.map(surveyId =>
  • Related