allenA
Convex Community3y ago
11 replies
allen

Paginated query briefly returning empty array

I'm observing a defective behavior where a newly mounted paginated query briefly returns an empty array before returning a result set. The expected behavior is that it returns undefined until results are ready. This results in my app rendering an empty state view instead of a loading view while this query is in flight.

There is some async linked document population that occurs, but I'm unsure why that would impact the query resolution.

The basic design of my query is as follows:

export default query(
  async ({ db }, opts) => {
    const results = await db
      .query('messages')
      .paginate(opts);

    const page = await Promise.all(
      results.page.map(async (item) => {
        return {
          ...item,
          focus: (await db.get(item.focusId)) as Doc<'focuses'>,
          fromUser: (await db.get(item.fromUserId)) as Doc<'users'>,
        };
      }),
    );

    return {
      isDone: results.isDone,
      continueCursor: results.continueCursor,
      page,
    };
  },
);
Was this page helpful?