Next.js Discord

Discord Forum

Search Params & get value date type

Answered
Ojos Azules posted this in #help-forum
Open in Discord
Ojos AzulesOP
I want to set initialData to my form the params the i get but my dateParam is string and i can't set it to defaultValues. Any ideas how to convert it to date? Thanks

  const timeSlot = searchParams.get('timeSlot');
  const barberParam = searchParams.get('barber');
  const dateParam = searchParams.get('day');
  


  const form = useForm<z.infer<typeof AppointmentsSchema>>({
    resolver: zodResolver(AppointmentsSchema),
    defaultValues: appointments
      ? {
          ...appointments,
        }
      : {
          clientName: "",
          clientPhone: "",
          clientEmail: "",
          serviceId: "",
          barberId: "",
          day:  dateParam || undefined,
          time: "",
        },
  }); 
Answered by Turkish Van
Based on the output error, I would say You have to make sure dateParam, or whichever other variable it says for, is not null.
View full answer

8 Replies

Ojos AzulesOP
No overload matches this call.
  Overload 1 of 4, '(value: string | number | Date): Date', gave the following error.
    Argument of type 'string | null' is not assignable to parameter of type 'string | number | Date'.
      Type 'null' is not assignable to type 'string | number | Date'.
  Overload 2 of 4, '(value: string | number): Date', gave the following error.
    Argument of type 'string | null' is not assignable to parameter of type 'string | number'.
      Type 'null' is not assignable to type 'string | number'.
I tried it but i get this error
Wed May 15 2024 00:25:30 GMT+0200 (GMT+02:00)
This is the value of dateParam
Answer
Ojos AzulesOP
Yes you have right
Thank you