TripleSpeeder
TripleSpeeder•11mo ago

How can i map/extend the results of a paginatedQuery?

I'm running a paginated query on a lookup table that returns objects with reference to the actual document. How can i do this? Below is the query i tried to use, but when calling loadMore i get this error:
InvalidCursor: Tried to run a query starting from a cursor, but it looks like this cursor is from a different query.
InvalidCursor: Tried to run a query starting from a cursor, but it looks like this cursor is from a different query.
Relevant part from the query:
// get user. If no userId is provided, use currently logged-in user
const user = feedUserId
? await db.get(feedUserId)
: await getCurrentUser(ctx);

const personalFeedItemsPage = await db
.query("personalFeed")
.withIndex("byUserId", (q) =>
q.eq("userId", user?._id || ("" as Id<"users">)),
)
.paginate(paginationOpts);

// map results from personalFeedItems to actual feedItems
const itemsPage = pruneNull(
await asyncMap(personalFeedItemsPage.page, (personalFeedItem) =>
db.get(personalFeedItem.feedItemId),
),
);

return {
...personalFeedItemsPage,
page: itemsPage,
};
// get user. If no userId is provided, use currently logged-in user
const user = feedUserId
? await db.get(feedUserId)
: await getCurrentUser(ctx);

const personalFeedItemsPage = await db
.query("personalFeed")
.withIndex("byUserId", (q) =>
q.eq("userId", user?._id || ("" as Id<"users">)),
)
.paginate(paginationOpts);

// map results from personalFeedItems to actual feedItems
const itemsPage = pruneNull(
await asyncMap(personalFeedItemsPage.page, (personalFeedItem) =>
db.get(personalFeedItem.feedItemId),
),
);

return {
...personalFeedItemsPage,
page: itemsPage,
};
I'm assuming the problem is that I'm returning a page of Doc<"feedItem"> instead of `Doc<"PersonalFeedItem">... What would be a better approach here?
3 Replies
TripleSpeeder
TripleSpeederOP•11mo ago
Bonus question: Any elegant idea for my handling of user being null? ( q.eq("userId", user?._id || ("" as Id<"users">)),) I'm doing this because everytime the query runs once with getCurrentUser() returning null, although I'm definitely logged in 😕. Looks somehow similar to the problem @Igor Kotua described in https://discord.com/channels/1019350475847499849/1209086079303426059...
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
lee
lee•11mo ago
You're mapping the results correctly. The InvalidCursor error happens if the query's index range changes between requests, so it's probably caused by the user?.id || "" which is causing the second page to be fetched with a different user id than the first page. When you render this query, is it possible to put it within a <Authenticated> component?
TripleSpeeder
TripleSpeederOP•11mo ago
Ah, makes perfectly sense. Thank you 🙂