簡體   English   中英

Gatsby 節點錯誤:拋出“必須提供源”錯誤

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

我在 Gatsby 中創建動態頁面,從 Fauna 中提取數據。 我在 gastby-node 中有一個查詢拋出錯誤“必須提供源”,但該查詢在 GraphiQL 中有效。 我在下面包含了 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
        },
    })
})
}

今天我遇到了同樣的錯誤,過了一段時間才弄清楚。 一個愚蠢的錯誤。

graphql是一個 function 需要使用查詢作為參數( graphql(`...`) )調用。 我把它誤認為是我在阿波羅中使用的graphql-taggql`...`

這應該工作

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,
      },
    })
  })
}

希望這可以幫助 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM