I am currently working on an API route called app/api/posts/route.ts
, which is responsible for fetching all posts from my database using Prisma ORM. In the localhost environment, the database is hosted on my local PostgreSQL server. However, in production, the database is located on render.com
. To fetch the data, I utilize axios within a function defined in app/page.tsx
. Here is the code snippet:
import Card from "@/components/Card";
import axios from "axios";
const getPosts = async () => {
const url =
process.env.NODE_ENV === "development"
? "http://localhost:3000/api/posts"
: "https://dropconnect.vercel.app/api/posts";
const res = await axios.get(url);
const data = await res.data;
return data;
};
export default async function Home() {
const posts = await getPosts();
return (
<div>
{posts.length === 0 ? (
<div>
<p>No Posts have been made yet</p>
</div>
) : (
<div>
{posts.map((post: any) => (
<div key={post.id}>
<Card
id={post.id}
title={post.title}
content={post.content}
comments={post.comments}
noComments={post.noComments}
noLikes={post.noLikes}
name={post.author.name}
image={post.author.image}
/>
</div>
))}
</div>
)}
</div>
);
}
The code for my /app/api/posts/route.ts
is as follows:
import { prisma } from "@/lib/database";
import { NextResponse } from "next/server";
export async function GET(req: Request) {
const allPosts = await prisma.post.findMany({
include: {
comments: true,
author: {
select: {
name: true,
image: true,
},
},
},
});
return NextResponse.json(allPosts);
}
Additionally, here is my schema.prisma
:
// Your unique Prisma schema file,
// for more information visit the official docs at: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Post {
// Model definition for Post table
}
...
// Additional model definitions like Comment, Account, User, etc.
...
Currently, everything works smoothly when running in the localhost environment: https://i.stack.imgur.com/RCq9T.png
However, upon deploying to GitHub, Vercel encounters an AxiosError with the following details:
... (error log details here)
If you have insights or suggestions on this issue, your help would be greatly appreciated.