【问题标题】:TypeError: Cannot read properties of undefined (reading 'document')TypeError:无法读取未定义的属性(正在读取“文档”)
【发布时间】:2022-04-17 05:33:38
【问题描述】:

在一个项目中使用 qraphql(JavaScript graphql-request 库)并遇到了 typeError。代码如下:

import { request, gql } from 'graphql-request'
const graphqlAPI = process.env.NEXT_PUBLIC_GRAPHCMS_ENDPOINT
export const getPosts = async () => {
    const query = gql`
    query MyQuery {
        postsConnection {
          edges {
            node {
              author {
                bio
                name
                id
                photo {
                  url
                }
              }
              createdAt
              slug
              title
              excerpt
              featuredImage {
                url
              }
              categories {
                name
                slug
              }
            }
          }
        }
      }
 `     
    const result = await request(graphqlAPI, query)

    return result.postsConnection.edges
}

错误说请求的文档参数有问题。

【问题讨论】:

    标签: javascript reactjs graphql next.js graphql-js


    【解决方案1】:

    没关系,因为 Next_endpoint 没有定义,现在一切正常!

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    2 个错误:

    1. 您在此处用于获取数据的方法已过时。
    2. 此处缺少端点,您可以在帐户的 graphcms 中找到它:settings/access/api access /endpoints

    删除命令

    const graphqlAPI = process.env.NEXT_PUBLIC_GRAPHCMS_ENDPOINT
    

    以及与之关联的 .env 文件。 之后使用以下代码:

    import { gql } from 'graphql-request';
    import { GraphQLClient } from 'graphql-request';
    
    export const getPosts = async () => {
        
        // new endpoint
        const graphQLClient = new GraphQLClient(
            endpoint // here add your endpoint
        );
    
        const query = gql`
        query MyQuery {
            postsConnection {
                edges {
                    node {
                        author {
                            bio
                            name
                            id
                            photo {
                                url
                            }
                        }
                        createdAt
                        slug
                        title
                        excerpt
                        featuredImage {
                            url
                        }
                        categories {
                            name
                            slug
                        }
                }
            }
            }
        }
        `
        const result = await graphQLClient.request(query)
        return result.PostsConnection;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2022-08-08
      • 2022-07-22
      • 1970-01-01
      • 2022-09-12
      • 2021-11-26
      相关资源
      最近更新 更多