You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gatsby中通过Fetch获取JSON动态创建节点时构建报错的问题求助

Gatsby中通过Fetch获取JSON动态创建节点时构建报错的问题求助

大家好,我现在在Gatsby里尝试通过gatsby-node.js获取JSON数据来创建节点,但遇到了个问题——用fetch请求数据的时候会出现构建报错,可是直接从本地路径加载JSON文件就完全正常。有没有大佬能给我指指路,告诉我怎么改用fetch来实现,而不是直接读取本地文件呀?

我目前用本地路径加载JSON的代码是这样的:

const path = require("path")
const pages = require("./pages.json")

exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions
  const queryResults = pages

  const productTemplate = path.resolve(`src/templates/product.js`)
  queryResults.forEach(node => {
    createPage({
      path: `${node.page}`,
      component: productTemplate,
      context: {
        // This time the entire product is passed down as context
        product: node,
      },
    })
  })
}

备注:内容来源于stack exchange,提问作者KnapLelijk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 12:48:07