如何仅在Sanity.io中使用特定的slug过滤一个数据?
数据:
[
{
"name": "Gates of Olympus",
"slug": {
"_type": "slug",
"current": "gates-of-olympus"
}
},
{
"name": "Floating Dragon",
"slug": {
"_type": "slug",
"current": "floating-dragon"
}
},
{
"name": "Buffalo King Megaways",
"slug": {
"_type": "slug",
"current": "buffalo-king-megaways"
}
},
{
"name": "Fruit Party",
"slug": {
"_type": "slug",
"current": "fruit-party"
}
}
]
如何仅查询使用slug of-olympus
的对象?
代码:
export const getServerSideProps = async ({params}:any) => {
const query = `*[_type=="game"]{
name,
slug,
}`;
const games = await sanityClient.fetch(query);
return {
props: {
games,
},
};
};
通过上下文获得SLUG( params.game
)。
我也尝试了
*[_type=="game" && slug.current == ${params.game}] but still returns all data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用引号包裹$ {params.game}。像这样的“ $ {params.game}”。它将起作用
Wrap ${params.game} with the quotes. Like this "${params.game}". It will work
您可以撤回所有数据,但是第一个或第一项数据是您在查询末尾搜索[0]末尾搜索的数据,以获取第一个值,您应该是可靠的,例如
*[_ type ==“ game”&& slug.current =='$ {params.game}'] [0]
ref
转到由JS Mastery Skip教授到1:21:27教授的视频,他开始解释如何获得当前的slug/product https://www.youtube.com/watch?v=4mokfxyxfsu&t = 5153S
You get back all the data but the first one or first item in that array of data is the one your searching for so at the end of your query put [0] at the end to get the first value you should be solid eg
*[_type=="game" && slug.current == '${params.game}'][0]
Ref
go to this video which is taught by js mastery skip to 1:21:27 he starts explaining how to get the current slug/product https://www.youtube.com/watch?v=4mOkFXyxfsU&t=5153s