Home > Enterprise >  Cannot read properties of undefined (reading 'replace')
Cannot read properties of undefined (reading 'replace')

Time:07-23

I have problem with mailChimp integration. On localhost all works good but on deploy i have error. Cannot read properties of undefined (reading 'replace'). This is my interface


interface NewsletterFormValues {
  EMAIL: string;
}

and my function:

const {
    handleSubmit,
    register,
    formState: { errors },
  } = useForm<NewsletterFormValues>();
  const url = process.env.NEXT_PUBLIC_MAILCHIMP_URL as string;
  const { message, handleSubmit: mailChimpSubmit } = useMailChimpForm(url);
  const emailError = errors["EMAIL"];
  const onSubmit = (fields: NewsletterFormValues) => {
    mailChimpSubmit(fields as Params);
  };

and this is my input for email adress

<input
                    {...register("EMAIL", {
                      required: true,
                      pattern:
                        /^(([^<>()[\]\\.,;:\s@"] (\.[^<>()[\]\\.,;:\s@"] )*)|(". "))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9] \.) [a-zA-Z]{2,}))$/,
                    })}
                    className="w-full lg:w-[458px] h-14 placeholder-black text-[16px] outline-0 py-[18px] pl-4 lg:mr-[24px] border-[1px] border-[#DDDDDC]"
                    placeholder="Email address"
                  />

CodePudding user response:

It appears process.env.NEXT_PUBLIC_MAILCHIMP_URL (and therefore url) is undefined, which causes the error when the undefined url is passed to useMailChimpForm.

  • Related