Sanity post 显示所有类别,而不仅仅是与 post 相关的类别

Sanity posts display all categories, not just ones associated with post

我正在使用 React 和 Sanity 创建一个博客,我想在每个 post 下方显示与每个 post 关联的类别。我能够显示类别,但它显示所有类别,而不仅仅是与每个特定 post 关联的类别。我怎样才能让每个 post 只显示与其关联的类别?

这是我的代码:

import React, { useEffect, useState } from 'react'
import client from '../client'
import BlockContent from '@sanity/block-content-to-react'
import { Link } from 'react-router-dom'

function Main() {
    const [posts, setPosts] = useState([])

    useEffect(() => {
        client.fetch(
            `*[_type == "post"] {
                title,
                slug,
                body,
                author,
                mainImage {
                    asset -> {
                        _id,
                        url
                    },
                    alt
                },
                publishedAt,
                "categories": categories[]->title
            }`
        ).then((data) => setPosts(data))
         .catch(console.error)
    }, [])

    return (
        <div className='grid lg:grid-cols-3 md:grid-cols-2 gap-8 m-4 '>
            <div>
                {posts.slice(0, 1).map((p, i) => (
                    <Link to = {`/blog/${p.slug.current}`} className=''>
                        <article key = {p.slug.current} className=''>
                            <img src = {p.mainImage.asset.url} alt = {p.title} className='' />
                            <div>
                                <p className='font-bold text-xl text-secondary'>{p.title}</p>
                                <div className=''>
                                    <p className='text-sm'>By Brandon Pyle | {new Date(p.publishedAt).toLocaleDateString()}</p>
                                    {posts.map((c, i) => (
                                        <p className='inline'>{c.categories}, </p>
                                    ))}
                                </div>
                            </div>
                        </article>
                    </Link>
                ))}
            </div>

            <div className='my-[-16px]'>
                {posts.slice(1, 4).map((p, i) => (
                    <Link to = {`/blog/${p.slug.current}`} className='col-start-2'>
                        <article key = {p.slug.current} className='flex my-4'>
                            <img src = {p.mainImage.asset.url} alt = {p.title} className='w-auto h-auto max-h-[80px]' />
                            <div>
                                <p className='font-bold text-xl text-secondary'>{p.title}</p>
                                <p className='text-sm'>By Brandon Pyle | {new Date(p.publishedAt).toLocaleDateString()}</p>
                                <div>
                                    {posts.map((c, i) => (
                                        <p className='inline'>{c.categories}, </p>
                                    ))}
                                </div>
                            </div>
                        </article>
                    </Link>
                ))}
            </div>
        </div>
    )
}

export default Main

感谢@cfm,我找到了解决方案:

Your query looks good. But you are mapping posts again inside the .map of posts. I would expect you to do p.categories.map in the inner map, if you wanted only this posts categories titles.

按照他的建议,我把posts.map换成了p.categories.map,问题就解决了!这是完整的固定代码:

import React, { useEffect, useState } from 'react'
import client from '../client'
import BlockContent from '@sanity/block-content-to-react'
import { Link } from 'react-router-dom'

function Main() {
    const [posts, setPosts] = useState([])

    useEffect(() => {
        client.fetch(
            `*[_type == "post"] {
                title,
                slug,
                body,
                author,
                mainImage {
                    asset -> {
                        _id,
                        url
                    },
                    alt
                },
                publishedAt,
                "categories": categories[]->title
            }`
        ).then((data) => setPosts(data))
         .catch(console.error)
    }, [])

    return (
        <div className='grid lg:grid-cols-3 md:grid-cols-2 gap-8 m-4 '>
            <div>
                {posts.slice(0, 1).map((p, i) => (
                    <Link to = {`/blog/${p.slug.current}`} className=''>
                        <article key = {p.slug.current} className=''>
                            <img src = {p.mainImage.asset.url} alt = {p.title} className='' />
                            <div>
                                <p className='font-bold text-xl text-secondary'>{p.title}</p>
                                <div className=''>
                                    <p className='text-sm'>By Brandon Pyle | {new Date(p.publishedAt).toLocaleDateString()}</p>
                                    {p.categories.map((c, i) => (
                                        <p className='inline'>{c}, </p>
                                    ))}
                                </div>
                            </div>
                        </article>
                    </Link>
                ))}
            </div>

            <div className='my-[-16px]'>
                {posts.slice(1, 4).map((p, i) => (
                    <Link to = {`/blog/${p.slug.current}`} className='col-start-2'>
                        <article key = {p.slug.current} className='flex my-4'>
                            <img src = {p.mainImage.asset.url} alt = {p.title} className='w-auto h-auto max-h-[80px]' />
                            <div>
                                <p className='font-bold text-xl text-secondary'>{p.title}</p>
                                <p className='text-sm'>By Brandon Pyle | {new Date(p.publishedAt).toLocaleDateString()}</p>
                                <div>
                                    {p.categories.map((c, i) => (
                                        <p className='inline'>{c}, </p>
                                    ))}
                                </div>
                            </div>
                        </article>
                    </Link>
                ))}
            </div>
        </div>
    )
}

export default Main