0

我按照教程进行操作,到了最后一部分,您将博客文章放在页面上,页面结束时只有一个 H1 标记,没有任何文章,有人能看一下代码并告诉我原因吗它可能不起作用?

import Link from 'next/link'
import groq from 'groq'
import client from '../client'

const Index = (props) => {
    const { posts = [] } = props
    return (
      <div>
        <h1>Welcome to a blog!</h1>
        {posts.map(
          ({ _id, title = '', slug = '', _updatedAt = '' }) =>
            slug && (
              <li key={_id}>
                <Link href="/post/[slug]" as={`/post/${slug.current}`}>
                  <a>{title}</a>
                </Link>{' '}
                ({new Date(_updatedAt).toDateString()})
              </li>
            )
        )}
      </div>
    )
}

Index.getInitialProps = async () => ({
    posts: await client.fetch(groq`
      *[_type == "post" && publishedAt < now()]|order(publishedAt desc)
    `)
})

export default Index
4

1 回答 1

1

我发现了这个问题,因为我遇到了同样的问题。

快速解决。

(页面/index.js)

我替换了这个:

*[_type == "post" && publishedAt < now()]|order(publishedAt desc)

进入这个:

*[_type == "post"]|order(publishedAt desc)

说明 如果您像我一样学习本教程,则您没有输入日期,示例中的查询更复杂。

为了更好地理解,我使用了 Vision ( http://localhost:3333/vision ) 部分来复制第一条 GROQ 行。然后去掉多余的参数。

希望这可以帮助!

于 2020-07-05T03:18:28.033 回答