【问题标题】:Cannot destructure property of {intermediate value} as it is undefined无法解构 {intermediate value} 的属性,因为它是未定义的
【发布时间】:2022-01-08 18:06:21
【问题描述】:

我刚刚开始使用graphql,因为我已经将我的NEXTJS 应用程序与strapi 集成在一起。但我收到了这条错误消息“无法解构‘(中间值)’的属性‘数据’,因为它是未定义的。”

我按照本教程进行操作 - enter link description here

只是将其修改为我想要的。这是我的 graphql -

query {
  posts {
    data {
      attributes {
        heading
      }
    }
  }
}

这是我的 vs 代码 -

export async function getStaticProps() {
  const client = new ApolloClient({
    url: 'http://localhost:1337/graphql/',
    cache: new InMemoryCache(),
  })

  const { data } = await client.query({
    query: gql`
      query {
        posts {
          data {
            attributes {
              heading
            }
          }
        }
      }
    `,
  })

  return {
    props: {
      posts: data.posts,
    },
  }
}

完整代码

import { ApolloClient, InMemoryCache, gql } from '@apollo/client'

export default function Blog({ posts }) {
  console.log('posts', posts)
  return (
    <div>
      {posts.map(post => {
        return (
          <div>
            <p>{posts.heading}</p>
          </div>
        )
      })}
    </div>
  )
}

export async function getStaticProps() {
  const client = new ApolloClient({
    url: 'http://localhost:1337/graphql/',
    cache: new InMemoryCache(),
  })

  const { data } = await client.query({
    query: gql`
      query {
        posts {
          data {
            attributes {
              heading
            }
          }
        }
      }
    `,
  })

  return {
    props: {
      posts: data.posts,
    },
  }
}

我真的不知道从哪里开始。

【问题讨论】:

  • 我一直在玩它。而且,它没有连接到我的 graphql 并访问数据,因此数组始终为空。
  • 代码导出异步函数 getStaticProps() { const client = new ApolloClient({ url: 'localhost:1337/graphql', cache: new InMemoryCache() }); const { stuff } = await client.query ({ query: gql `query GetPosts { posts{ data{ attributes { heading } } } } ` });返回 { 道具: { 帖子: stuff.posts } } }

标签: graphql next.js strapi


【解决方案1】:
  1. 首先检查您是否从 API 接收到空数据。
  • 如果是数组,请检查其长度。
  • 如果是它的对象,请创建这样的函数来检查对象。
function isObjectEmpty(obj) {
 return (
   !!obj && // ? null and undefined check
   Object.keys(obj).length === 0 &&
   obj.constructor === Object
 )
}

export default isObjectEmpty
  • 如果数据为空,则返回 notFound 属性为 true 以显示您的 404 页面。
// This function gets called at build time
export async function getStaticProps({ params, preview = false }) {
  // fetch posts

  // check validity data
  return isObjectEmpty(pageData)
    ? { notFound: true }
    : {
        props: {
          posts
        }
      }
}
  1. 其次添加故障保护机制,例如使用optional-chaining 来安全访问嵌套值/属性。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

export default function Blog({ posts }) {
  console.log('posts', posts)
  return (
    <div>
      {posts?.length && posts?.map(post => {
        return (
          <div>
            <p>{posts?.heading}</p>
          </div>
        )
      })}
    </div>
  )
}

【讨论】:

  • 它显示的是一个空数组,它没有访问graphql url
  • 我认为现在这个问题是 Graphql 特有的。试试这 2 件事。 1. 尝试使用 URL url: 'http://localhost:1337' and url: 'localhost:1337/graphql'` 2. 尝试使用fetch API 直接发出 n/w 请求。 const data = await fetch(GRAPHQL_URL, { method: 'GET' }) .then(res=&gt;res.json()) .catch(error =&gt; console.error('Network Error:', error)) const posts = data?.posts
猜你喜欢
  • 1970-01-01
  • 2022-11-29
  • 2020-10-07
  • 1970-01-01
  • 2021-03-08
  • 2021-05-11
  • 2021-04-04
  • 2021-08-30
  • 1970-01-01
相关资源
最近更新 更多