T3 trpc depdent query is failing ?

I have this code , my third question getQuestion useQuery is failing not sure why, I read about from online but adding enabled did not solve it, the error is TypeError: Cannot read properties of undefined (reading 'length'), can anyone help me on this ?
const event = api.slido.getSlido.useQuery({ id: eventId });
const poll = api.poll.getAll.useQuery(
{ slidoId: eventId },
{ enabled: !!event },
);

const { data: eventData, isLoading } = event;
const { data: pollData } = poll;
const currentPoll = pollData?.find((item) => item.slidoId === eventId);
const pollId = currentPoll?.id;

console.log({ currentPoll }, currentPoll?.id);

const question =
currentPoll?.id &&
api.question.getQuestions.useQuery(
{
pollId: currentPoll.id,
},
{
enabled: !!pollId,
},
);
const event = api.slido.getSlido.useQuery({ id: eventId });
const poll = api.poll.getAll.useQuery(
{ slidoId: eventId },
{ enabled: !!event },
);

const { data: eventData, isLoading } = event;
const { data: pollData } = poll;
const currentPoll = pollData?.find((item) => item.slidoId === eventId);
const pollId = currentPoll?.id;

console.log({ currentPoll }, currentPoll?.id);

const question =
currentPoll?.id &&
api.question.getQuestions.useQuery(
{
pollId: currentPoll.id,
},
{
enabled: !!pollId,
},
);
2 Replies
Sturlen
Sturlen2mo ago
without the full error message it's difficult to tell what's going on. you might be running into problems with dependent queries so the docs might be helpful: https://tanstack.com/query/latest/docs/framework/react/guides/dependent-queries
Dependent Queries | TanStack Query React Docs
useQuery dependent Query Dependent (or serial) queries depend on previous ones to finish before they can execute. To achieve this, it's as easy as using the enabled option to tell a query when it is ready to run:
Sturlen
Sturlen2mo ago
also renaming your hooks like event and poll to useEvent and usePoll would make it clear that they are hooks and need to be treated as such