Background
After learning getServerProps, I tried building a project that returns some data from postgres using prisma, but for some reason, when I try to map the posts array ( See below ), it returns the array as undefined. Enevn I believe I followed syntax rules on the Doc page, no luck so far. I have been stuck for long time and I don't know why and what i did wrong. At least VS code recognize posts as array, but getServerProps function seems it never gets run.Thanks for reading, lemme know your thoughts on this.
Summary of my problem
- I don't know why the array (
postsvariable) is undefined when it is recognized inside of the function. - I don't know why getServerProps seems to be not called
Note:
Prisma and postgres are working in this project
Posts.js
import { PrismaClient } from '@prisma/client'
function Posts({ posts }) {
return (
<div>
<h1 className="text-4xl block text-gray-500 font-bold pt-5 text-center">
Posts
</h1>
<div>
{posts.map((post) => {
<h3>{post.title}</h3>;
})}
</div>
</div>
);
}
export const getServerSideProps = async () => {
const prisma = new PrismaClient()
const posts = await prisma.post.findMany()
return { props: { posts } }
}
export default Posts
schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Post {
id Int @id @default(autoincrement())
title String
content String
}
Error I get:
Error message

Prisma Studio

File structure

