Passing Arguments
Hey all, finally found more time to learn Convex. I'm stuck on passing an argument, where for example, I have a table full of email addresses that acts as a whitelist - and I'd like my server to pass an email string to the function, Convex check the email does/doesn't exist, and return true/false.
Here's my server app and my functions;
And my whitelistFunctions.js
Get the error
*I should note that I started to simplify/hack checkEmail right back to the bone, so that essentially I was just returning the passed argument - but still couldn't figure it out.
9 Replies
Instead of as a property on ctx, args are passed as a second argument to the Convex function
instead of
yah, I think I got this sorted....
So now that I have the argument passing - I'll find some time after cooking dinner to write the function to check the email passed in the argument against all emails in the table, and return true of false if it's found.
I have a working example done in Airatble, but differently - where I query my Airtable from my server via a CURL containing a formula query. I'm not sure if the same thing can be done in Convex? Or if, I'm just writing the Convex cloud function to do the specific task of finding/matching the given email.
For Airtable, you can construct queries with this codepen;
https://codepen.io/airtable/full/MeXqOg
It's more common to write the Convex cloud function to do the specific task of finding/matching an email from a specific table with a specific query. You could include arguments for which table to query or what filters to use, but it's hard to represent an entire query as as argument.
Cool, so I'm on the right track 🙂
I wish you could poke chatGPT with all your updated docs - would make learning a bit quicker. 😛 But I say that about everything now. If only OpenAI could cook my dinner too 😦
You'll likely end up doing something like this:
- write a query that returns whether an email is in the whitelist
- write an action that takes the argument, calls the query to look for the email, reads the result and then makes a fetch() or use some JavaScript API to send the email.
something like
(postmark is just an example, one of many client libraries used to send emails)
Ok, so I'm starting to understand Convex, and I'm really excited by this.
Here's my cloud function;
And I set my server with promise.allSettled()
server.mjs
And it... just works! I'm really sold on Convex, I can't thank you enough Tom.
One last question, to check against multiple fields, is it typical to stack filters() as such? Technically it works, but I do like to check my methods...
One side question I have, is that I'm reading about limits - that filter is limited to 16384 documents/records.
I'm not fully up-to-scratch on table limits and batch queries, but say for example, I had 20,000 email addresses, and I had to query for an existing email in that 20,000 - how do I build a query function to cater for a large table beyond 16384 documents? I started reading about indexing, but although I could get my Table schema, I wasn't too sure on how to update it with the indexing mechanism.
Nor then how to update my query to leverage the index (which... I'm guessing resolves querying a table with more than 16384 documents?)
(oh gawd, I just realised, I'm that "one last question..." guy. I'm so sorry!)
Yup, Indexes are the answer. https://docs.convex.dev/database/indexes/
Add to schema
.index("byEmail", ["email"])
Query:
Indexes | Convex Developer Hub
Indexes are a data structure that allow you to speed up your
Cool - been reading through the docco. Convex is awesome!