可以从UI中的strapi显示显示的数据,但可以使用Next.js在控制台中看到它们

发布于 2025-02-08 03:03:33 字数 3084 浏览 0 评论 0原文

我正在尝试使用NextJS作为前端从Strapi检索数据。问题是我无法在UI上显示它们,但是我可以在控制台中看到它们。

我使用GraphQl来查询数据:

query {
    menus(filters: {language: {code: {eq: "en"}}}) {
        data {
            attributes {
              Entry {
                title
                link
              }
            }
        }
    }
}

,我得到了:

{
  "data": {
    "menus": {
      "data": [
        {
          "attributes": {
            "Entry": [
              {
                "title": "home",
                "link": "/home.html"
              },
              {
                "title": "classes",
                "link": "./calss.html"
              },
              {
                "title": "events",
                "link": "/events.html"
              },
              {
                "title": "news",
                "link": "/news.html"
              },
              {
                "title": "reosurces",
                "link": "./resources.html"
              },
              {
                "title": "links",
                "link": "./links.html"
              }
            ]
          }
        }
      
      ]
    }
  }
}

,这就是UI处理的方式:

const mainMenuAdapter = (menuData) => {
  
  return menuData.map(md => {
    const entry = md['attributes']['Entry']
    console.log('title',  entry)
      return {
        link: entry.link,
        title: entry.title,
      }
    })
  }

使用usecontext;

 const { menuData } = useContext(GlobalContext)
  const menus = mainMenuAdapter(menuData)

我在此处 是处理映射的方式。

   <div className="bg-green-400 relative filosofia_regular bg-grayDark flex-wrap md:flex flex-row items-end md:justify-around p-2 lg:justify-evenly text-center mx-auto w-full">
        {!!menus && menus.map((menu, i) => {
          return (
            <div key={i}>
              <Link href="/">
                <a className="text-gray-100 px-2 text-sm lg:text-2xl xl:text-4xl whitespace-nowrap">
                  {t(menu.title)}
                </a>
              </Link>
              {i < menus.length - 1 && (
                <a className="text-white">|</a>
              )}
            </div>
          );
        })}
      </div>

这是控制台。

(6) [{…}, {…}, {…}, {…}, {…}, {…}]
0:
link: "/home.html"
title: "home"
__typename: "ComponentEntryComEntry"
[[Prototype]]: Object
1: {title: 'classes', link: './calss.html', __typename: 'ComponentEntryComEntry'}
2: {title: 'events', link: '/events.html', __typename: 'ComponentEntryComEntry'}
3: {title: 'news', link: '/news.html', __typename: 'ComponentEntryComEntry'}
4: {title: 'reosurces', link: './resources.html', __typename: 'ComponentEntryComEntry'}
5: {title: 'links', link: './links.html', __typename: 'ComponentEntryComEntry'}
length: 6
[[Prototype]]: Array(0)

有人可以告诉我我缺少什么吗?

谢谢

I am trying to retrieve data from strapi using the NextJs as the front-end. The problem is that I can't display them on the UI, but I can see them in the console.

I used Graphql for querying the data using this commend:

query {
    menus(filters: {language: {code: {eq: "en"}}}) {
        data {
            attributes {
              Entry {
                title
                link
              }
            }
        }
    }
}

, I got this:

{
  "data": {
    "menus": {
      "data": [
        {
          "attributes": {
            "Entry": [
              {
                "title": "home",
                "link": "/home.html"
              },
              {
                "title": "classes",
                "link": "./calss.html"
              },
              {
                "title": "events",
                "link": "/events.html"
              },
              {
                "title": "news",
                "link": "/news.html"
              },
              {
                "title": "reosurces",
                "link": "./resources.html"
              },
              {
                "title": "links",
                "link": "./links.html"
              }
            ]
          }
        }
      
      ]
    }
  }
}

and this is how the UI handled:

const mainMenuAdapter = (menuData) => {
  
  return menuData.map(md => {
    const entry = md['attributes']['Entry']
    console.log('title',  entry)
      return {
        link: entry.link,
        title: entry.title,
      }
    })
  }

I use useContext;

 const { menuData } = useContext(GlobalContext)
  const menus = mainMenuAdapter(menuData)

here is how the mapping is handled.

   <div className="bg-green-400 relative filosofia_regular bg-grayDark flex-wrap md:flex flex-row items-end md:justify-around p-2 lg:justify-evenly text-center mx-auto w-full">
        {!!menus && menus.map((menu, i) => {
          return (
            <div key={i}>
              <Link href="/">
                <a className="text-gray-100 px-2 text-sm lg:text-2xl xl:text-4xl whitespace-nowrap">
                  {t(menu.title)}
                </a>
              </Link>
              {i < menus.length - 1 && (
                <a className="text-white">|</a>
              )}
            </div>
          );
        })}
      </div>

and here is the console.

(6) [{…}, {…}, {…}, {…}, {…}, {…}]
0:
link: "/home.html"
title: "home"
__typename: "ComponentEntryComEntry"
[[Prototype]]: Object
1: {title: 'classes', link: './calss.html', __typename: 'ComponentEntryComEntry'}
2: {title: 'events', link: '/events.html', __typename: 'ComponentEntryComEntry'}
3: {title: 'news', link: '/news.html', __typename: 'ComponentEntryComEntry'}
4: {title: 'reosurces', link: './resources.html', __typename: 'ComponentEntryComEntry'}
5: {title: 'links', link: './links.html', __typename: 'ComponentEntryComEntry'}
length: 6
[[Prototype]]: Array(0)

Can anyone please tell me what I am missing?

Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文