0

What I'm trying to do is fetch an array of objects, then fetch data related to the first object in the array.

const progress = useSelector(state => state.progress);
const appInfo = useSelector(state => state.appInfo);

const {
    data: steps,
    isLoading: isStepsLoading,
    isSuccess: isStepsSuccess
} = useGetAllStepsQuery()

const {
  data: questions
} = useGetQuestionsQuery({app_id: appInfo.id, step_parameter: steps[progress.currentStepIndex].step_parameter}, {skip: isStepsLoading})

The problem I'm having is that attempting to extract the step_parameter from the steps array is throwing a Cannot read properties of undefined (reading '0') error: in other words, it's as if the useGetQuestionsQuery is being called before the useGetAllStepsQuery is finished. I've added a skip parameter to the request, per this answer, and tried various values with it (e.g. !isStepsSuccess), none of them resolve this problem. I've also tried assigning the step_parameter conditionally, like this:

step_parameter: isStepsSuccess ? steps[progress.currentStepIndex].step_parameter : ''

Which does prevent the error, but it also appears to prevent the questions call from ever being made. Any help would be appreciated!

ngm01
  • 1
  • 2
  • Hmm. I would expect that the `skip` approach would work here. Can you put together a CodeSandbox that shows what you're doing there? – markerikson Aug 27 '22 at 02:20

0 Answers0