reactjs - 从 Sanity 获取数据返回未定义(Next.js + Sanity)
问题描述
我有一个页面,我想在其中添加一些实际情况。这些实际情况将首先在 Sanity 中设置,然后通过 Next.js 获取。
我的理智模式
export default{
name:"actuality",
title:"Aktuality",
type:"document",
fields:[
{
name:"headline",
title:"Nadpis",
type:"string"
},
{
name:"publishedAt",
title:"Datum zveřejnění",
type:"datetime"
},
{
name:"body",
title:"Text",
type:"blockContent"
}
],
preview:{
select:{
title:"headline",
}
}
}
问题在于获取数据。
如果我这样做,它会起作用,但只会返回理智中的第一个现实
export const getServerSideProps = async (pageContext: any) => {
const query = `*[ _type == "actuality"][0]`;
const recipe = await client.fetch(query);
console.log(recipe);
if (!recipe) return { props: null, notFound: true };
else
return {
props: {
headline: recipe.headline,
publishedAt: recipe.publishedAt,
body: recipe.body,
},
};
};
但是如果我删除 [0] 它会抛出错误:“原因:undefined
无法序列化为 JSON。请使用null
或省略此值。”
为了获得一系列实际情况,我需要更改什么?
解决方案
一些事情:
- 如果你删除它会返回一个数组
[0]
,你可以只返回数据,不管数组与否。
props: {
data: recipe
}
- 如果您想返回带有 obj 值的单个数据作为多个道具
props: {...recipe}
推荐阅读
- javascript - 使用 Google Apps 脚本和 JavaScript 从 google 表格反映 HTML 中的选项值
- php - 将json转换为多级数组
- python - 如何在循环中将 r 前缀包含到字符串中?
- installation - Atmel Studio 7.0 及其安装程序抛出错误:“找不到一个或多个组件。请重新安装应用程序。”
- mysql - MySql中的WHEN条件生成多列而不是一列?
- c# - 如何在 C# 表单应用程序中从 MDVR 播放 .264 格式的原始视频文件
- c# - 无法加载文件或程序集“Microsoft.ML.Api”
- php - 如何打印避免重复天数的时间表?
- c# - 如何访问 ViewBag 中的动态对象成员
- kubernetes - 我们可以增加 configmap 的大小来存储大小超过 1 MB 的二进制数据吗?