stevanfreeborn
stevanfreeborn
CCConvex Community
Created by stevanfreeborn on 9/17/2023 in #show-and-tell
conve-X
No description
3 replies
CCConvex Community
Created by stevanfreeborn on 9/16/2023 in #support-community
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
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,
},
};
})
);
}
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 }
);
const postsPager = usePaginatedQuery(
api.posts.getPostsBySearchTerm,
{ term },
{ initialNumItems: PAGE_SIZE }
);
19 replies
CCConvex Community
Created by stevanfreeborn on 9/11/2023 in #support-community
Select or Project Method
Does convex have any built in for doing some sort of select statement or project method when querying? I see how I can make a query, collect the documents, and return them after mapping over the documents. But that doesn't really work if you want to use a paginated query. It seems like in this case you'd have to implement your own pagination in the query.
8 replies
CCConvex Community
Created by stevanfreeborn on 9/11/2023 in #support-community
Joining and Paging
Is there an established pattern for both joining data and paging through it using the built-in paginated queries? I can see how you could implement it yourself in the typical pattern of passing page parameters to a query that does the joining, but the usability of the paginated queries is really nice.
3 replies
CCConvex Community
Created by stevanfreeborn on 9/10/2023 in #support-community
Mutating Arrays
What is the recommended approach for mutating an array stored in the database? Like if I’m building up an array of followers and I want to patch the document where these followers are stored by removing a specific element and then put it back in the database. Do you just query the document at the beginning of the mutation, modify the array and then patch it back into the database?
3 replies
CCConvex Community
Created by stevanfreeborn on 9/9/2023 in #support-community
Collaborative Editing Experience
Does anyone here have any experiencing using convex with some editor for doing collaborative editing? I've spent quite a lot of hours on trying to get something function without any success. I've found lots of implementations of this type of feature using socket.io with quilljs etc., but I'm having a hard time understanding how to translate that to convex paradigm. I seem to end up in this place where I've got a sort of infinite loop going on. Would love to talk with anyone who has experience implementing this kind of functionality.
7 replies
CCConvex Community
Created by stevanfreeborn on 9/9/2023 in #support-community
How to call public query from action?
This seems like it should be super obvious, but I can't see to understand how to reference the public query within the runQuery method.
7 replies
CCConvex Community
Created by stevanfreeborn on 9/9/2023 in #support-community
Poll Data
I understand that when you call useQuery you automatically get the benefits of subscribing to changes in the data, but is there any means to pull on this data programmatically? I’m interested in building a collaborative editor, but I’m having a hard time translating how so might implement that with something like socket.io to convex.
3 replies