简体   繁体   English

错误:如何从 Next.js 中的 getStaticProps 序列化数据

[英]Error: How to serialize data from getStaticProps in Next.js

I am trying to fetch data from an Api but for sorry i've received this Error:我正在尝试从 Api 获取数据,但很抱歉我收到了此错误:

Server Error Error: Error serializing .results returned from getServerSideProps in "/".服务器错误错误:序列化从“/”中的getServerSideProps返回的.results时出错。 Reason: undefined cannot be serialized as JSON.原因: undefined不能序列化为JSON。 Please use null or omit this value.请使用null或省略此值。

my code我的代码

The props you're passing contain an object with a key of value undefined .您传递的道具包含一个 object ,其键值为undefined A quick solution would be一个快速的解决方案是

return {
 props: {
   results: JSON.parse(JSON.stringify(request.results)); 
 }
}

JSON.stringify will remove those undefined values. JSON.stringify将删除那些未定义的值。 You can try it out in your browser console: JSON.stringify({someKey:undefined})您可以在浏览器控制台中试用: JSON.stringify({someKey:undefined})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:如何序列化来自 getStaticProps 的数据:Next.js - Error: How to serialize data from getStaticProps : Next.js Next.js ISR 将附加数据从 getStaticPaths 传递给 getStaticProps - Next.js ISR pass additional data to getStaticProps from getStaticPaths 如何在 _app.js 中使用 getStaticProps 在 Next.js 的每个页面中显示从 api 获取的数据 - How to use getStaticProps in _app.js for showing data fetched from api in every pages in Next.js Next.js getStaticProps 将数据作为对象传递 - Next.js getStaticProps passing data as object 在 getStaticProps function 中序列化 Next.js 时出错? - Error serializing Next.js in getStaticProps function? 在 Next.js 中,序列化 getStaticProps 时出错? - In Next.js, Error serializing getStaticProps? 来自 getStaticProps 与 Next.js 的无效响应 - Invalid response from getStaticProps with Next.js Next.js - 如何使用 getStaticProps 获取菜单数据,然后将其传递给布局? - Next.js - How to use getStaticProps to fetch Menu data and then pass it to the layout? 无法从 Next.js 中的 getStaticProps 传递数组 - Cannot pass array from getStaticProps in Next.js 如何使用 getStaticProps 为 Next.js 中的 static 站点导入 ts 和 html? - How to import ts and html with getStaticProps for a static site in Next.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM