#String into ID !
22 messages · Page 1 of 1 (latest)
Welcome! So onEventIdChange calls setEventId with a plain string, not an id type? Is it coming from outside of Convex?
Thanks! Yes, I'm fetching it with a QR reader
Ah okay. You could set the type of the useState to an id, and when you call setEventId, cast the string to an id with as
const [ticketId, setTicketId] = useState("")
const ticket = useQuery(api.tickets.getTicketWithDetails, ticketId ? { ticketId: ticketId as Id<"tickets"> } : 'skip')
Simpler ^^
Passing 'skip' when ticketId is falsy keeps the query from running
awesome, let me try that
I did fixed the error with the type, but now the ticket returning null, before it wasn't.
function ValidateTicket({
event,
}: {
event: Doc<"events"> & {
metrics: Metrics;
};
}) {
const [eventId, setEventId] = useState<string>("");
const [ticketId, setTicketId] = useState("");
const ticket = useQuery(
api.tickets.getTicketWithDetails,
ticketId ? { ticketId: ticketId as Id<"tickets"> } : "skip"
);
if (!ticket) {
return null;
}
actly, it's not waiting for the qr code input with the ticket ID and going straight to null.. I'll sort that out! Thank you so much for the help!
Also linking https://docs.convex.dev/database/document-ids#serializing-ids
Another approach here when dealing with IDs coming from external sources is to have your function take in a v.string() instead of a v.id() and use db.normalizeId + db.get to check it's a valid ID (potentially throwing a specific error if not).
Pointing this out just in case -- useQuery returns undefined when it's loading or skipped (and might also return null if you return null or undefined from your function), and if (!ticket) would catch both of these, vs. something like if (ticket === undefined) { return "Loading..." } else if (ticket === null) { return null }
this is still in the same error I believe... On npm run dev the build is fully working, but getting this error when trying to deploy with Vercel.. I believe it's the same Id issue.. any thoughts?
Type "{ event: { _id: Id<"events">; _creationTime: number; imageStorageId?: Id<"_storage"> | undefined; is_cancelled?: boolean | undefined; name: string; description: string; location: string; eventDate: number; price: number; totalTickets: number; userId: string; } & { ...; }; }" is not valid.
What line of code does the type error reference
I can't manage to find it ;/ it says it's in that page but I don't see this event: ...
I think it might be referencing to this event, but it's weird that everything is working well on dev, but not on vercel deploy
Nevermind, just found the issue! Really glad for all the help!
No problem! Can you share the solution in case anyone else ends up here in the future?
Sure, it wasn't anything related to Convex tho. I just removed the full event argument in the validate function. I'll paste the code tomorrow.