数据来源:
[
{
"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 gates-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.
2条答案
按热度按时间w9apscun1#
用引号将${params.game}括起来。就像这样“${params.game}"。它将起作用
kkbh8khc2#
您可以取回所有数据,但该数据数组中的第一个或第一项是您要搜索的数据,因此在查询的末尾将[0]放在末尾以获取第一个值,您应该是实心的,例如
*[_type=="game" && slug.current == '${params.game}'][0]
参考去这个视频,这是教js掌握跳到1:21:27,他开始解释如何获得当前的鼻涕虫/产品https://www.youtube.com/watch?v=4mOkFXyxfsU&t=5153s