Next.js Discord

Discord Forum

Error 504

Answered
Broad-snouted Caiman posted this in #help-forum
Open in Discord
Avatar
Broad-snouted CaimanOP
not sure why there are any errors because my application, when run locally, doesnt have any errors. this only happes when i deploy it to vercel.

this is the relevant part of the code:

api/contact - route.js

import connectDB from "@/app/lib/mongodb";
import Contact from "@/app/models/contact";
import { NextResponse } from "next/server";
import mongoose from "mongoose";

export async function POST(req) {
  const { fullname, message } = await req.json();

  try {
    await connectDB();
    await Contact.create({ fullname, email, message });

    return NextResponse.json({
      msg: ["Message sent successfully"],
      success: true,
    });
  } catch (error) {
    if (error instanceof mongoose.Error.ValidationError) {
      let errorList = [];
      for (let e in error.errors) {
        errorList.push(error.errors[e].message);
      }
      console.log(errorList);
      return NextResponse.json({ msg: errorList });
    } else {
      return NextResponse.json({ msg: ["Unable to send message."] });
    }
  }
}



contactform.jsx -

"use client";

import { useState } from "react";

export default function ContactForm() {
  const [fullname, setFullname] = useState("");
  const [message, setMessage] = useState("");
  const [error, setError] = useState([]);
  const [success, setSuccess] = useState(false);

  const handleSubmit = async (e) => {
    e.preventDefault();

    const res = await fetch("api/contact", {
      method: "POST",
      headers: {
        "Content-type": "application/json",
      },
      body: JSON.stringify({
        fullname,
        message,
      }),
    });

    const { msg, success } = await res.json();
    setError(msg);
    setSuccess(success);

    if (success) {
      setFullname("");
      setMessage("");
    }
  };

  return(*the form*)
Image
Answered by Kokoni
Your function might have timed out because of your connectDB function not connecting to your database.
View full answer

8 Replies

Avatar
Kokoni
Your function might have timed out because of your connectDB function not connecting to your database.
Answer
Avatar
Broad-snouted CaimanOP
yep i fixed it. i forgot to allow the ip address 🤦‍♂️
Mark Solution
/Mark Solution
how do i do thattt
Avatar
josh
right click then
Image
Avatar
Kokoni
@Broad-snouted Caiman ^
Avatar
Broad-snouted CaimanOP
o thanks