我在 getStaticProps
内部调用时收到以下错误,但我不知道为什么:
Error: Error serializing `.lingo` returned from `getStaticProps` in "/".
Reason: `undefined` cannot be serialized as JSON.
我已将完整的应用代码放在 CodeSandbox 上。它将无法访问 API,但它确实显示了定义的位置。
当我在 GraphQL playground 上运行以下查询时,我得到了预期的响应:
query {
allTerms {
id
term
slug
lead
}
}
您可以看到此查询包含在沙箱的 lingo.service.js
目录中的 modules/lingo/services
中,但主页出现 Error serializing
错误。是我的函数 export async function getAll()
不正确还是我在 getStaticProps
中调用它是错误的?
答案 0 :(得分:0)
await getAll()
很可能返回 undefined
,它不是可序列化的 JSON。默认为 null
将是解决问题的一种方法。
export async function getStaticProps(context) {
return {
props: { lingo: (await getAll()) ?? null },
};
}