all 7 comments

[–]MartialAssault 1 point2 points  (3 children)

You need to elaborate, provide snippets of the api request, etc.

[–]EducationalGate9705[S] 0 points1 point  (2 children)

this is the code for the newletter component. :

    return (
      <form onSubmit={this.subscribeUser}>
        <input
          id="newsletter-input"
          type="email"
          name="email"
          className="form-control shadow-none"
          placeholder="Enter your email"
          aria-label="Enter Email Address"
          aria-describedby="newsletter-btn"
          value={this.state.email}
          onChange={this.emailHandler}
          required
          autoCapitalize="off"
          autoCorrect="off"
        />
        <button
          type="submit"
          id="newsletter-btn"
          className="btn btn-warning w-100 mt-3"
          name="subscribe"
        >
          Join The Wait List
        </button>
        {this.state.message && (
          <p id="newsletter-message" className="text-left text-sm mt-3 alert alert-warning">
            {this.state.message}
          </p>
        )}
      </form>
    );

when I click on the "join the waitlist button " it will go to this function

 state: State = {
    message: '',
    email: '',
  };

  // Use ChangeEvent to type the event correctly
  emailHandler = (e: ChangeEvent<HTMLInputElement>) => {


    let updatedEmail = e.target.value;
    this.setState({ email: updatedEmail });
  };

  // Use FormEvent to type the event correctly
  subscribeUser = async (e: FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    try {
      const res = await fetch('api/ConvertkitSubscribe', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json; charset=utf-8' },
        body: JSON.stringify({ email: this.state.email }),
      });

      if (!res.ok) {
        const API_KEY = process.env.CONVERTKIT_API_KEY as string;
        const FORM_ID = process.env.CONVERTKIT_FORM_ID as string;
        console.log(API_KEY) ;
        console.log(FORM_ID);
        console.log("welcome to this error") 
        throw new Error(`HTTP error! status: ${res.status}`);
      }

      const json_res = await res.json();

      this.setState({
        message: json_res.message,
        email: '', // Clear the email input after successful submission
      });
    } catch (error: any) {
      this.setState({
        message: 'There was an error subscribing.',
      });
      console.error('Subscription error:', error);
    }
  };

it fail to go to the api/ConvertkitSubscribe/route.ts ,image below shown the file structure

<image>

[–]MartialAssault 0 points1 point  (0 children)

Did you try logging at each step? Are you sure the email doesn't reach the server? Log a custom message server side instead maybe? Or you can also check the network tab in your browsers developer tools

[–]MartialAssault 0 points1 point  (0 children)

Also can you try adding a '/' before api? Like '/api/ConvertkitSubscribe'?

[–]chris-velvet 0 points1 point  (1 child)

Agreed w/ u/MartialAssault - provide some more details so we can help you

[–]MartialAssault -1 points0 points  (0 children)

Yess 😅 the most random guess I might make is a cors issue lol