gpt4 book ai didi

reactjs - 尝试在 gatsby-node 中创建页面时,我收到此构建错误

转载 作者:行者123 更新时间:2023-12-04 04:20:22 25 4
gpt4 key购买 nike

 ERROR #11321  PLUGIN

"gatsby-node.js" threw an error while running the onCreateNode lifecycle:

Cannot read property 'replace' of undefined

25 | const { createNodeField } = actions;
26 | if (node.internal.type === 'MarkdownRemark') {
> 27 | const slug = createFilePath({ node, getNode, basePath: 'content' });
| ^
28 | createNodeField({
29 | node,
30 | name: 'slug',

File: gatsby-node.js:27:18



TypeError: Cannot read property 'replace' of undefined

- path.js:54 slash
[netcreative0]/[gatsby-core-utils]/dist/path.js:54:15

- create-file-path.js:41 module.exports
[netcreative0]/[gatsby-source-filesystem]/create-file-path.js:41:61

- gatsby-node.js:27 Object.exports.onCreateNode
C:/code/netcreative0/gatsby-node.js:27:18

- api-runner-node.js:247 runAPI
[netcreative0]/[gatsby]/dist/utils/api-runner-node.js:247:41

- api-runner-node.js:387 resolve
[netcreative0]/[gatsby]/dist/utils/api-runner-node.js:387:13

- new Promise

- api-runner-node.js:386 runPlugin
[netcreative0]/[gatsby]/dist/utils/api-runner-node.js:386:10

- api-runner-node.js:340 module.exports
[netcreative0]/[gatsby]/dist/utils/api-runner-node.js:340:24

- next_tick.js:68 process._tickCallback
internal/process/next_tick.js:68:7

这是该文件中的所有代码。我找到了这个链接 https://github.com/datocms/gatsby-source-datocms/issues/52 但它似乎对删除 getNode 没有帮助。在路径插件中,是这个斜线。我注意到当我 console.log 时路径未定义,但我不知道该怎么做。
**function slash(path) {
const isExtendedLengthPath = /^\\\\\?\\/.test(path);

if (isExtendedLengthPath) {
return path;
}

return path.replace(/\\/g, `/`);
}**

const path = require('path');
const DirectoryNamedWebpackPlugin = require('directory-named-webpack-plugin');
const { createFilePath } = require('gatsby-source-filesystem');

exports.onCreateWebpackConfig = ({
stage,
getConfig,
rules,
loaders,
actions,
}) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
plugins: [
new DirectoryNamedWebpackPlugin({
exclude: /node_modules/,
}),
],
},
});
};

exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
const slug = createFilePath({ node, getNode, basePath: 'slugs' });
console.log(slug);
createNodeField({
node,
name: `slug`,
value: slug,
});
}
};

我的代码的后半部分看起来像:
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`)
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
console.log(node)
createPage({
path: node.fields ? node.fields.slug : '',
component: path.resolve(`./src/template/project.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.fields ? node.fields.slug : '',
},
})
});
}

最后一个字段为空并引发错误。不知道为什么它甚至在那里?我可以很容易地设置一个条件,但默认值是什么?有什么建议么?见下文
{ fields: { slug: '/about/content/' } }
{ fields: { slug: '/home/intro/' } }
{ fields: { slug: '/slugs/goldparent/' } }
{ fields: { slug: '/slugs/ipadmaster/' } }
{ fields: null }

最佳答案

删除内容丰富的插件后,我能够看到更有用的错误消息。
创建新页面时,.md 文件中缺少某些图像,因此我的模板页面中的这一行出现错误

data.frontmatter.featuredImage.childImageSharp.fluid <-

我在图像标签中添加了一个三元条件,然后在此之后构建
<Img
style={{ width: '70%' }}
fluid={
data.frontmatter.featuredImage
? data.frontmatter.featuredImage.childImageSharp.fluid
: {}
}
alt="Large Image"
/>

关于reactjs - 尝试在 gatsby-node 中创建页面时,我收到此构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59536569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com