PierreP
Convex Community14mo ago
29 replies
Pierre

.paginate combined with .withIndex is causing an error

let say I have two tables in my scheme that represents a user that lives in a city and posts that are made in a city.
All the code below could be better arranged, but this is for the simplicity of the example:

const userTable = defineTable({
  name: v.string()
  cityId: v.string()
})

const postTable = defineTable({
message: v.string()
cityId: v.string()
})


Let's say I want to display all the posts from the city of the user, so this is how I achieve it:

export const getPostForUser = query({
args: {
  userId: v.id("user")
  paginationOpts: paginationOptsValidator
},
handler: async (ctx, args) => {
  const user = await ctx.db.get(args.userId)
  if (user == null) { throw new ConvexError("Error") }
  const storiesByUsers = await ctx.db
    .query('post')
    .withIndex('by_cityId', q => q.eq('cityId', user.cityId))
    .paginate(args.paginationOpts);
}
})


The problem is that when a user change his city by patching his data, the getPostForUser function returns an error:
Uncaught Error: InvalidCursor: Tried to run a query starting from a cursor, but it looks like this cursor is from a different query.

I think this happens because the args haven't changed but the result has, given it has been retriggered by the inner data change from the user table.
A way to circumvent this would be to instead pass the city of the user as an argument of the function, so the query result changes when the args change.
However, I would like to improve the ergonomics here: I feel that the code above looks good and it is unfortunate not being able to rely on a table for a paginated query. I don't think it makes sense this function returns an error conceptually.
Was this page helpful?