String into ID !

That feels so simple and yet I can't manage to nail it on my code :/ Can anybody help? I have this ticketId value in a string and I just need to pass it as an argument in the correct format! What should I do?
No description
15 Replies
erquhart
erquhart3w ago
Welcome! So onEventIdChange calls setEventId with a plain string, not an id type? Is it coming from outside of Convex?
Pedro Mameluque
Pedro MameluqueOP3w ago
Thanks! Yes, I'm fetching it with a QR reader
erquhart
erquhart3w ago
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')
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
Pedro Mameluque
Pedro MameluqueOP3w ago
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;
}
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!
sshader
sshader3w ago
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).
Document IDs | Convex Developer Hub
Create complex, relational data models using IDs.
sshader
sshader3w ago
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 }
Pedro Mameluque
Pedro MameluqueOP3w ago
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.
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.
erquhart
erquhart3w ago
What line of code does the type error reference
Pedro Mameluque
Pedro MameluqueOP3w ago
I can't manage to find it ;/ it says it's in that page but I don't see this event: ...
No description
Pedro Mameluque
Pedro MameluqueOP3w ago
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
No description
Pedro Mameluque
Pedro MameluqueOP3w ago
Nevermind, just found the issue! Really glad for all the help!
erquhart
erquhart3w ago
No problem! Can you share the solution in case anyone else ends up here in the future?
Pedro Mameluque
Pedro MameluqueOP3w ago
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.
erquhart
erquhart3w ago
Ah okay, no bigs then

Did you find this page helpful?