Next.js Discord

Discord Forum

Error during deployment due to a bad call API

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
Hey, I hope u are doing well, I have this error but I don't know how to fix it, it will be great if I can have your help guys.

Error :



This is my code :

import React from "react";
import { useSearchParams, useRouter, usePathname } from "next/navigation";
import CardCalendar2 from "./CardCalendar2";
import {fetchDates} from "../api/Date/route"; // Ajustement de l'import

async function CardCalendar() {
  const datess = await fetchDates();
  const newdate = JSON.parse(JSON.stringify(datess));

  return (
    <>
      <CardCalendar2 newdate={newdate} />
    </>
  );
}

export default CardCalendar;
Answered by Dwarf Crocodile
Move the fetchDates function to a file that isn't a route.ts or a page.tsx file and it will work. You can't export non-nextjs functions like your fetchDates from a page.tsx or route.ts.
View full answer

5 Replies

Sun bearOP
And this is my route.ts :

import { NextResponse } from "next/server";

import Date from "@/app/(models)/DatePost";

export async function POST(req: Request) {
  try {
    const Datedata = await req.json(); 
    await Date.create(Datedata);
    console.log(Datedata);
    return NextResponse.json({ message: "Date Created" }, { status: 201 });
  } catch (err) {
    return NextResponse.json({ message: "Error", err }, { status: 500 });
  }
}

export async function fetchDates() {
  try {
    return await Date.find();
  } catch (err) {
    return NextResponse.json({ message: "Error for Date", err }, { status: 500 });
  }
}
All works in localProd, but when I deploy on vercel it don't works anymore
Dwarf Crocodile
Move the fetchDates function to a file that isn't a route.ts or a page.tsx file and it will work. You can't export non-nextjs functions like your fetchDates from a page.tsx or route.ts.
Answer
Dwarf Crocodile
np