Gatsby运行报错TypeError: createNodeId is not a function该如何解决?
错误原因
Gatsby 的 exports.sourceNodes API 仅接收一个对象类型的入参,所有可用的工具方法和配置项都包含在这个对象中。你当前的写法把 createNodeId、createContentDigest 拆成了独立的第二、第三个入参,实际上这两个方法并没有被正确传入,因此调用时会抛出「不是函数」的类型错误。
修复方案
修改 exports.sourceNodes 的入参写法,从第一个对象中解构出需要的方法即可,修正后的代码如下:
// 入参改为从第一个对象中解构所需字段 exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => { const res = await axios.get("https://jsonplaceholder.typicode.com/posts") const posts = res.data posts.forEach(post => { const node = { title: post.title, body: post.body, id: createNodeId(`Post-${post?.id}`), parent: null, children: [], internal: { type: "Post", contentDigest: createContentDigest(post), content: JSON.stringify(post) } } actions.createNode(node) }) }
内容的提问来源于stack exchange,提问作者JorgeL
相关产品推荐
相关产品推荐

