如何仅在Sanity.io中使用特定的slug过滤一个数据?

发布于 2025-02-09 14:31:42 字数 1028 浏览 2 评论 0 原文

数据:

[
  {
    "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.

Data:

[
  {
    "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"
    }
  }
]

How do I query only objects with slug gates-of-olympus ?

Code:

export const getServerSideProps = async ({params}:any) => {
    
    const query = `*[_type=="game"]{
    name,
    slug,
    }`;
  
    const games = await sanityClient.fetch(query);
  
    return {
      props: {
        games,
      },
    };
  };

slug is obtained through context (params.game).

I also tried,

*[_type=="game" && slug.current == ${params.game}] but still returns all data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2025-02-16 14:31:42

用引号包裹$ {params.game}。像这样的“ $ {params.game}”。它将起作用

Wrap ${params.game} with the quotes. Like this "${params.game}". It will work

弥繁 2025-02-16 14:31:42

您可以撤回所有数据,但是第一个或第一项数据是您在查询末尾搜索[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文