为什么我收到“错误:错误序列化从 getStaticProps 返回的 ___”?

时间:2021-01-18 18:51:11

标签: json next.js apollo getstaticprops

我在 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 中调用它是错误的?

1 个答案:

答案 0 :(得分:0)

await getAll() 很可能返回 undefined,它不是可序列化的 JSON。默认为 null 将是解决问题的一种方法。

export async function getStaticProps(context) {
  return {
    props: { lingo: (await getAll()) ?? null },
  };
}