Search Params & get value date type
Answered
Ojos Azules posted this in #help-forum
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.8 Replies
@Ojos Azules 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
tsx
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: "",
},
});
Turkish Van
If that date, in form of string, is in the appropriate format, something like
You can find more about the
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
YYYY-MM-DD, You could use JavaScript Date object.new Date(dateParam)You can find more about the
Date object here:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
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
@Ojos Azules tsx
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'.
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.Answer
Ojos AzulesOP
Yes you have right
Thank you