我正在使用 Prisma 和 Next.js。当我尝试从 Prisma 检索内容时getStaticProps
它确实获取了数据,但我无法将其传递给主要组件。
export const getStaticProps = async () => {
const prisma = new PrismaClient();
const newsLetters = await prisma.newsLetters.findMany();
console.log(newsLetters);
return {
props: {
newsLetters: newsLetters,
},
};
};
正如您在此图中看到的,它正在获取并打印内容。
但是当我通过时,我收到以下错误,将其作为道具传递
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.
如果您正在使用打字稿,你不能改变类型createdAt
为字符串或数字,如下所示:
newsLetter.createdAt = newsLetter.createdAt.toString();
// Error: Type 'string' is not assignable to type 'Date'.
相反,您可以在 JSON.parse 中使用 JSON.stringify 来创建可序列化对象:
export const getStaticProps = async () => {
const prisma = new PrismaClient();
const newsLetters = await prisma.newsLetters.findMany();
return {
props: {
newsLetters: JSON.parse(JSON.stringify(newsLetters)) // <===
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)