简体   繁体   English

Gatsby 节点错误:抛出“必须提供源”错误

[英]Gatsby-node error: “Must Provide Source” error being thrown

I'm creating dynamic pages in Gatsby, pulling data from Fauna.我在 Gatsby 中创建动态页面,从 Fauna 中提取数据。 I've got a query in gastby-node that is throwing an error "Must provide Source", but the query works in GraphiQL.我在 gastby-node 中有一个查询抛出错误“必须提供源”,但该查询在 GraphiQL 中有效。 I've include gatsby-node.js below.我在下面包含了 gatsby-node.js。

exports.createPages = async function({actions, graphql}){
    const {data} = await graphql`
    query {
        fauna {
            allCompanies {
                data {
                    slug
                }
            }
        }
    }
    `

data.fauna.allCompanies.data.forEach(edge => {
    const slug = edge.slug
    actions.createPages({
        path: slug,
        component: require.resolve("./src/components/products.js"),
        context:{
            slug
        },
    })
})
}

Today I encountered the same error and after some time figured it out.今天我遇到了同样的错误,过了一段时间才弄清楚。 A stupid mistake tbh.一个愚蠢的错误。

graphql is a function that needs to be called with the query as the parameter ( graphql(`...`) ). graphql是一个 function 需要使用查询作为参数( graphql(`...`) )调用。 I was mistaking it for graphql-tag which I used in apollo as gql`...`我把它误认为是我在阿波罗中使用的graphql-taggql`...`

This should work这应该工作

exports.createPages = async function ({ actions, graphql }) {
  /* 
     you have to pass the template literal query
     in the function graphql(`...`)

  */
  const { data } = await graphql(`
    query {
      fauna {
        allCompanies {
          data {
            slug
          }
        }
      }
    }
  `)

  data.fauna.allCompanies.data.forEach(edge => {
    const slug = edge.slug
    actions.createPages({
      path: slug,
      component: require.resolve("./src/components/products.js"),
      context: {
        slug,
      },
    })
  })
}

Hope this helps !希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 条纹异常错误(必须提供来源或客户) - Stripe Exception error (Must provide source or customer) 基于 graphql 查询创建 gatsby-node 页面 - create gatsby-node pages based on a graphql query 将 gatsby-node 文件重构为单独的文件不起作用 - Refactoring gatsby-node File into separate files not working gatsby-node 没有构建标签页面 - gatsby-node isn't building tags pages 在 gatsby-node 中创建自定义模式后,ChildImageSharp 为 null - ChildImageSharp is null after creating custom schema in gatsby-node 如何在'gatsby-node'中使用不同的模板创建多个页面? - how to create multiple pages using different templates in 'gatsby-node'? “无法读取未定义的属性‘页面’” - Gatsby 和 Prismic 在 gatsby-node 中为模板创建分页 - “Cannot read property 'page' of undefined” - Gatsby and Prismic creating pagination for template in gatsby-node 在 node-redis 客户端断开连接后重新连接时抛出错误 - Error being thrown on reconnection after disconnection in node-redis client GraphQL:错误:必须提供文档 - GraphQL: Error: Must provide Document 尝试为类别或标签构建模板页面时,我在 gatsby-node 中误解了什么? - What am I misunderstanding in gatsby-node when trying to build a template page for categories or tags?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM