我的.map()函数中的帖子未在浏览器上显示。

huangapple go评论60阅读模式
英文:

Posts in my .map() function are not displaying on browser

问题

在我的代码中,我已经反复运行了很多次,但是我无法找出问题所在。我正在按照一个使用 Next.js 13.4 和 Sanity.io 的博客构建教程进行操作。

在我的 BlogList 组件中,通过 console.log 打印出的 posts 在终端中显示得很好,但是当我将这些帖子映射到页面上时,页面上什么都不显示。

没有任何错误的迹象。如果您能再仔细查看一下代码,我会很感激。您可能会发现我忽略的东西。

page.tsx - 博客主页

import { draftMode } from "next/headers";
import { groq } from "next-sanity";
import { client } from "@/lib/sanity.client";
import BlogList from "@/components/blog/BlogList.tsx";
import BlogHeader from "@/components/blog/BlogHeader";
import PreviewSuspense from "@/components/studio/PreviewSuspense";
import PreviewBlogList from "@/components/studio/PreviewBlogList";

const query = groq`
  *[_type=='post'] {
    ...,
    _id,
    _createdAt,
    author->,
    "slug": slug.current
  } | order(_createdAt desc)
`;

export default async function page() {
  const { isEnabled } = draftMode();
  if (isEnabled) {
    return (
      <PreviewSuspense
        fallback={
          <div role="status">
            <p className="text-center text-lg animate-pulse text-blue-800">
              Loading Draft Data
            </p>
          </div>
        }
      >
        <PreviewBlogList query={query} />
      </PreviewSuspense>
    );
  }

  const posts = await client.fetch<Post[]>(query);
  // console.log(posts);
  return (
    <>
      <BlogHeader />
      <section
        id="blog"
        className="w-full xl:pt-10 xl:pb-24 pb-12 p-4 flex flex-col gap-10 xl:gap-0 align-middle items-center border-b-[1px] border-b-gray-300 dark:border-b-gray-500"
      >
        <BlogList posts={posts} />
      </section>
    </>
  );
}

BlogList.tsx - 博客列表组件 - 特别是在 .map() 函数中的内容没有显示在页面上。然而,在 .map() 函数之外的任何内容都会显示出来。

import Image from "next/image";
import { urlForImage } from "../../../sanity/lib/image";

interface Props {
  posts: Post[];
}

export default function BlogList({ posts }: Props) {
  console.log(posts);
  return (
    <>
      <div className="grid grid-cols-1 gap-8 mt-8 md:mt-16 md:grid-cols-2 xl:grid-cols-3">
        {posts.map((post) => {
          return (
            <div key={post._id} className="group cursor-pointer">
              <div className="relative w-full h-64 lg:h-80 group-hover:scale-105 drop-shadow-xl">
                <Image
                  className="object-cover object-center rounded-lg"
                  src={urlForImage(post.mainImage).url()}
                  alt={post.author.name}
                  fill
                />

                <h1>hello there</h1>

                <div className="absolute bottom-0 flex p-3 bg-white dark:bg-gray-900 ">
                  <Image
                    className="rounded-full"
                    src={urlForImage(post.author.image).url()}
                    alt={post.author.name}
                    width={10}
                    height={10}
                  />

                  <div className="mx-4">
                    <h1 className="text-sm text-gray-700 dark:text-gray-200">
                      {post.author.name}
                    </h1>
                    <p className="text-sm text-gray-500 dark:text-gray-400">
                      {new Date(post._createdAt).toLocaleDateString("en-US", {
                        day: "numeric",
                        month: "numeric",
                        year: "numeric",
                      })}
                    </p>
                  </div>
                </div>
              </div>

              <h1 className="mt-6 text-xl font-semibold text-gray-800 dark:text-white">
                {post.title}
              </h1>

              <hr className="w-32 my-6 text-blue-500" />

              <p className="text-sm text-gray-500 dark:text-gray-400">
                Lorem ipsum dolor sit amet consectetur adipisicing elit.
                Blanditiis fugit dolorum amet dolores praesentium, alias nam?
                Tempore
              </p>

              <a
                href="#"
                className="inline-block mt-4 text-blue-500 underline hover:text-blue-400"
              >
                Read more
              </a>
            </div>
          );
        })}
      </div>
    </>
  );
}

这是我对您的问题的翻译,如果您需要进一步的帮助,请随时告诉我。

英文:

I have run through my code countless times but I can't make out the problem. I am following a blog build tutotial with Nextjs 13.4 with Sanity.io.

In my BlogList component, a console log of my posts reveals all my post in the terminal alright, but when I map the post into the UI, nothing shows up on my page.

There is no indication of an error anywhere. I would love it if you could take a second look at the code. You might find something I missed.

page.tsx - Blog Home Page

import { draftMode } from &quot;next/headers&quot;;
import { groq } from &quot;next-sanity&quot;;
import { client } from &quot;@/lib/sanity.client&quot;;
import BlogList from &quot;@/components/blog/BlogList.tsx&quot;;
import BlogHeader from &quot;@/components/blog/BlogHeader&quot;;
import PreviewSuspense from &quot;@/components/studio/PreviewSuspense&quot;;
import PreviewBlogList from &quot;@/components/studio/PreviewBlogList&quot;;
const query = groq`
*[_type==&#39;post&#39;] {
...,
_id,
_createdAt,
author-&gt;,
&quot;slug&quot;: slug.current
} | order(_createdAt desc)
`;
export default async function page() {
const { isEnabled } = draftMode();
if (isEnabled) {
return (
&lt;PreviewSuspense
fallback={
&lt;div role=&quot;status&quot;&gt;
&lt;p className=&quot;text-center text-lg animate-pulse text-blue-800&quot;&gt;
Loading Draft Data
&lt;/p&gt;
&lt;/div&gt;
}
&gt;
&lt;PreviewBlogList query={query} /&gt;
&lt;/PreviewSuspense&gt;
);
}
const posts = await client.fetch&lt;Post[]&gt;(query);
// console.log(posts);
return (
&lt;&gt;
&lt;BlogHeader /&gt;
&lt;section
id=&quot;blog&quot;
className=&quot;w-full xl:pt-10 xl:pb-24 pb-12 p-4 flex flex-col gap-10 xl:gap-0 align-middle items-center border-b-[1px] border-b-gray-300 dark:border-b-gray-500&quot;
&gt;
&lt;BlogList posts={posts} /&gt;
&lt;/section&gt;
&lt;/&gt;
);
}

BlogList.tsx - Blog List Component - Nothing on this page especially content in the .map() function shows up on the page. Any Content outside of the .map() function shows up though.

import Image from &quot;next/image&quot;;
import { urlForImage } from &quot;../../../sanity/lib/image&quot;;
interface Props {
posts: Post[];
}
export default function BlogList({ posts }: Props) {
console.log(posts);
return (
&lt;&gt;
&lt;div className=&quot;grid grid-cols-1 gap-8 mt-8 md:mt-16 md:grid-cols-2 xl:grid-cols-3&quot;&gt;
{posts.map((post) =&gt; {
&lt;div key={post._id} className=&quot;group cursor-pointer&quot;&gt;
&lt;div className=&quot;relative w-full h-64 lg:h-80 group-hover:scale-105 drop-shadow-xl&quot;&gt;
&lt;Image
className=&quot;object-cover object-center rounded-lg&quot;
src={urlForImage(post.mainImage).url()}
alt={post.author.name}
fill
/&gt;
&lt;h1&gt;hello there&lt;/h1&gt;
&lt;div className=&quot;absolute bottom-0 flex p-3 bg-white dark:bg-gray-900 &quot;&gt;
&lt;Image
className=&quot;rounded-full&quot;
src={urlForImage(post.author.image).url()}
alt={post.author.name}
width={10}
height={10}
/&gt;
&lt;div className=&quot;mx-4&quot;&gt;
&lt;h1 className=&quot;text-sm text-gray-700 dark:text-gray-200&quot;&gt;
{post.author.name}
&lt;/h1&gt;
&lt;p className=&quot;text-sm text-gray-500 dark:text-gray-400&quot;&gt;
{new Date(post._createdAt).toLocaleDateString(&quot;en-US&quot;, {
day: &quot;numeric&quot;,
month: &quot;numeric&quot;,
year: &quot;numeric&quot;,
})}
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h1 className=&quot;mt-6 text-xl font-semibold text-gray-800 dark:text-white&quot;&gt;
{post.title}
&lt;/h1&gt;
&lt;hr className=&quot;w-32 my-6 text-blue-500&quot; /&gt;
&lt;p className=&quot;text-sm text-gray-500 dark:text-gray-400&quot;&gt;
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Blanditiis fugit dolorum amet dolores praesentium, alias nam?
Tempore
&lt;/p&gt;
&lt;a
href=&quot;#&quot;
className=&quot;inline-block mt-4 text-blue-500 underline hover:text-blue-400&quot;
&gt;
Read more
&lt;/a&gt;
&lt;/div&gt;;
})}
&lt;/div&gt;
&lt;/&gt;
);
}

Console Log for my Posts

[
{
slug: &#39;blog-development-with-next-13-4-and-sanity&#39;,
mainImage: { _type: &#39;image&#39;, alt: &#39;Cover Image&#39;, asset: [Object] },
publishedAt: &#39;2023-06-14T11:15:00.000Z&#39;,
_type: &#39;post&#39;,
categories: [ [Object], [Object] ],
title: &#39;Blog Development with Next 13.4 and Sanity&#39;,
author: {
name: &#39;Edward Opoku-Agyemang&#39;,
_updatedAt: &#39;2023-06-14T11:11:24Z&#39;,
slug: [Object],
image: [Object],
_createdAt: &#39;2023-06-14T11:11:24Z&#39;,
_rev: &#39;sKRwurG6U83UyjXGn2pQeT&#39;,
_type: &#39;author&#39;,
bio: [Array],
_id: &#39;46ec3787-817d-441f-be7b-eab31e9ed3db&#39;
},
_id: &#39;0fa6470e-f88f-46d7-a127-a932b0f148fb&#39;,
_updatedAt: &#39;2023-06-14T11:15:49Z&#39;,
_createdAt: &#39;2023-06-14T11:15:33Z&#39;,
_rev: &#39;TrPpVOhlpnRSDfzu76fyos&#39;,
body: [ [Object], [Object], [Object], [Object] ]
}
]

答案1

得分: 0

你在BlogList.tsx中的映射中没有返回任何内容。

{posts.map((post) => {

你只需要从映射的末尾删除 { 和 }。这将执行隐式返回并返回标记。

或者你需要这样做

{posts.map((post) => {
return (

)
}

英文:

You are not returning anything from the map in BlogList.tsx

{posts.map((post) => {

You just need to remove the { and the } from the end of the map. This will do an implicit return and return the markup.

Alternative you need to do

{posts.map((post) =&gt; {
return (&lt;div&gt;&lt;/div&gt;)
}

huangapple
  • 本文由 发表于 2023年6月15日 21:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定