#String into ID !

22 messages · Page 1 of 1 (latest)

earnest flare
#

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?

regal quarry
#

Welcome! So onEventIdChange calls setEventId with a plain string, not an id type? Is it coming from outside of Convex?

earnest flare
#

Thanks! Yes, I'm fetching it with a QR reader

regal quarry
#

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

earnest flare
#

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!

sturdy radish
#

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 }

earnest flare
#

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.
regal quarry
#

What line of code does the type error reference

earnest flare
#

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

earnest flare
#

Nevermind, just found the issue! Really glad for all the help!

regal quarry
earnest flare
#

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.