reactjs - 尝试在 gatsby-node 中创建页面时,我收到此构建错误
问题描述
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 : '',
},
})
});
}
最后一个字段为 null 并引发错误。不知道为什么它甚至在那里?我可以很容易地设置一个条件,但默认值是什么?有什么建议么?见下文
{ 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"
/>
推荐阅读
- c# - 在 IIS 上发布网站后数据库无法正常工作 - C# Web 服务
- flutter - 如何使用干净的架构执行登录身份验证
- if-statement - Stata:使用带有值标签的 if
- sql-server - 如何从 STRING_AGG 返回到常规表?
- javascript - 错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。请帮我解决这个错误
- python - 如何保存编辑的 PyQt5 TableWidget 单元格?
- openmp - 操作系统线程是否与不同 OpenMP 并行区域中的 thread_num 相关联?
- javascript - 如何使用数组获取输入名称的唯一标识符以将数据发送到数据库?
- python - 我对 Python 全局有疑问
- c++ - 设置对象相对于其他对象的位置