Sara
Sara23h ago

Proper Validation to getting all data created in the last week with `.withIndex` and pagination

I'm using luxon to do time calculations, and this is first time I've run into this issue, I have a usePaginatedQuery imported from cache provider from convex-helpers with next.js latest, and I'm running the following query below, the userId is coming from the ctx, and the DateTime is coming from luxon, built on top of index, when I add the last gte for the field _creationTime I get an infinite loop on my frontend Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. I've tested the query and its working for a sure, but somehow its causing my frontend to overreact,
export const listAllByUserId = authenticatedQuery({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, args_0) => {
const cutoff = DateTime.now().minus({ days: 7 }).toMillis();
const all = await ctx.db.query("shoppingLists")
++ .withIndex("by_creatorId", q => q.eq("creatorId", ctx.user._id).gte("_creationTime",cutoff))
-- .withIndex("by_creatorId", q => q.eq("creatorId", ctx.user._id))
.order("desc")
.paginate(args_0.paginationOpts);

return all
},
})
export const listAllByUserId = authenticatedQuery({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, args_0) => {
const cutoff = DateTime.now().minus({ days: 7 }).toMillis();
const all = await ctx.db.query("shoppingLists")
++ .withIndex("by_creatorId", q => q.eq("creatorId", ctx.user._id).gte("_creationTime",cutoff))
-- .withIndex("by_creatorId", q => q.eq("creatorId", ctx.user._id))
.order("desc")
.paginate(args_0.paginationOpts);

return all
},
})
9 Replies
Convex Bot
Convex Bot23h ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Sara
SaraOP23h ago
also just looked at my dashboard I'm not passing cursor, could it be because of the caching?
No description
erquhart
erquhart18h ago
Can you share the code where you're calling this in the client
Sara
SaraOP18h ago
// imports
import { useMountedState } from "@reactuses/core"
// based on the convex-helpers packages, we import this for next.js
import { usePaginatedQuery } from "convex-helpers/react/cache/hooks"

// inside the component itself
const firstMount = useMountedState()
const { results, isLoading, loadMore, status } = usePaginatedQuery(api.shopping.index.listAllByUserId, firstMount() ? {} : "skip", { initialNumItems: 7, })
// imports
import { useMountedState } from "@reactuses/core"
// based on the convex-helpers packages, we import this for next.js
import { usePaginatedQuery } from "convex-helpers/react/cache/hooks"

// inside the component itself
const firstMount = useMountedState()
const { results, isLoading, loadMore, status } = usePaginatedQuery(api.shopping.index.listAllByUserId, firstMount() ? {} : "skip", { initialNumItems: 7, })
I'm 60% sure its coming from the cached cursor
erquhart
erquhart18h ago
googles "reactuses" Ah gotcha I'd try regular usePaginatedQuery to confirm
Sara
SaraOP18h ago
nah its just basically a useRef that's updated on first mount well I kinda need it to be cached atm, if it keeps crashing I'd change it back
erquhart
erquhart18h ago
I mean just to try it and confirm whether the caching is your only problem
Sara
SaraOP17h ago
uh, then it was deff related to the cached cursor, I'm able to re-produce it when I update a mutation, but not on viewing elements this time
No description
Sara
SaraOP17h ago
I guess I'm using the other one for now then

Did you find this page helpful?