StevanS
Convex Community3y ago
18 replies
Stevan

Help Debugging

Would someone be able to help me debug this error:

Error: [CONVEX Q(posts.js:getPostsBySearchTerm)] Uncaught Error: Must provide arg 2 `value` to `search`
    at <anonymous> (../convex/posts.ts:196:35)
    at handler (../convex/posts.ts:193:23)

  Called by client


Here is my query:

export const getPostsBySearchTerm = query({
  args: { term: v.string(), paginationOpts: paginationOptsValidator },
  handler: async (ctx, args) => {
    const posts = await ctx.db
      .query('posts')
      .withSearchIndex('search_by_content', 
        q => q.search('content', args.term).eq('parentPostId', undefined)
      )
      .paginate(args.paginationOpts);

    const postsWithUser = await getPostsWithUsers({ ctx, posts: posts.page });

    return { ...posts, page: postsWithUser };
  },
});

async function getPostsWithUsers({
  ctx,
  posts,
}: {
  ctx: QueryCtx;
  posts: Doc<'posts'>[];
}) {
  return await Promise.all(
    posts.map(async post => {
      const user = await ctx.db.get(post.userId);

      if (user === null) {
        return {
          ...post,
          user: {
            _id: '' as Id<'users'>,
            _creationTime: 0,
            clerkUsername: null,
            clerkImageUrl: '',
            clerkUserId: '',
          },
        };
      }

      return {
        ...post,
        user: {
          _id: user._id,
          _creationTime: user._creationTime,
          clerkUsername: user.clerkUser.username,
          clerkImageUrl: user.clerkUser.image_url,
          clerkUserId: user.clerkUser.id,
        },
      };
    })
  );
}


And then calling it from the frontend

const postsPager = usePaginatedQuery(
  api.posts.getPostsBySearchTerm,
  { term },
  { initialNumItems: PAGE_SIZE }
 );
Was this page helpful?