KarlstensK
Convex Community3y ago
17 replies
Karlstens

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;

//server.mjs
import { ConvexHttpClient } from "convex/browser";
import { api } from "./convex/_generated/api.js";
import * as dotenv from "dotenv";
dotenv.config({ path: ".env.local" });

const client = new ConvexHttpClient(process.env["CONVEX_URL"]);

client.query(api.whitelistFunctions.get).then(console.log);

client
    .query(api.whitelistFunctions.checkEmail, { email: "sample@email.com" })
    .then(console.log); // This will print true if the email exists, otherwise false


And my whitelistFunctions.js
//whitelistFunctions.js
import { query } from "./_generated/server";
import { v } from "convex/values";

export const get = query({
    args: {},
    handler: async (ctx) => {
        return await ctx.db.query("whitelist").collect();
    },
});

export const checkEmail = query({
    args: { email: v.string() },

    handler: async (ctx) => {
        if (!ctx.args.email) {
            throw new Error("Email argument is missing or undefined");
        }
        return await ctx.args.email;
    },
});


Get the error
Error: Uncaught TypeError: Cannot read properties of undefined (reading 'email')
    at handler (../convex/whitelistFunctions.js:15:13)

    at ConvexHttpClient.query (file:///C:/DEV/VS%20Code/Convex/karlstensWebsite/node_modules/convex/dist/esm/browser/http_client.js:122:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)


*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.
Was this page helpful?