Background
在学习了getServerProps之后,我try 构建一个项目,使用prisma从postgres返回一些数据,但由于某种原因,当我try 映射posts数组(见下文)时,它返回未定义的array.Enevn我相信我遵循了文档页面上的语法规则,到目前为止运气不好.我被困了很长时间,我不知道为什么,我做错了什么.至少VS代码将POST识别为数组,但getServerProps函数似乎从未运行过.谢谢你的阅读,让我知道你对此的 idea .
Summary of my problem
- 我不知道为什么数组(
posts
变量)在函数内部被识别时没有定义. - 我不知道为什么getServerProps似乎没有被调用
Note:
Prisma和postgres正在这个项目中工作
帖子.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
架构.棱镜
// 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
}
获取错误: