查询返回错误“无法读取未定义的属性(读取'节点')”
Posted
技术标签:
【中文标题】查询返回错误“无法读取未定义的属性(读取\'节点\')”【英文标题】:Query returning error "Cannot read properties of undefined (reading 'nodes')"查询返回错误“无法读取未定义的属性(读取'节点')” 【发布时间】:2022-01-19 01:07:58 【问题描述】:我正在尝试编写一个页面查询以在 GatsbyJS 项目中返回一行文本和一个图像。我正在使用 Gatsby Image 插件以及 'gatsby-transformer-sharp' 和 'gatsby-plugin-sharp' 并且我不断收到一个错误,即从查询返回的节点未定义。
我的查询如下所示:
query ProjectsPage
allMarkdownRemark
nodes
frontmatter
featuredImg
childImageSharp
fluid
...GatsbyImageSharpFluid
title
给我错误的代码行:
const projects = data.projects.nodes
我的 gatsby-config.js 文件如下所示:
module.exports =
/* Your site config here */
plugins: [
'gatsby-transformer-remark',
'gatsby-transformer-sharp',
'gatsby-plugin-sharp',
resolve: `gatsby-source-filesystem`,
options:
name: `projects`,
path: `$__dirname/src/projects/`,
,
,
resolve: `gatsby-source-filesystem`,
options:
name: `images`,
path: `$__dirname/src/images/`,
,
,
],
siteMetadata:
title: 'example',
description: 'web dev portfolio',
copyright: 'example text'
,
非常感谢您对此提供任何帮助!
【问题讨论】:
什么是data
?可能不是您所期望的,因为 data.project
是 undefined
。
好电话,我不需要 data
它是我摆脱的其他东西的遗留物,但即使删除它,我仍然会遇到同样的错误
【参考方案1】:
从查询中获取节点的正确方法是:
const projects = data.allMarkdownRemark.nodes
如果您想按目录过滤结果并且只从项目中获取节点,您可以按照here 的答案之一并使用类似:
query ProjectsPage
allMarkdownRemark(filter: fileAbsolutePath: regex: "/(/projects/)/")
nodes
frontmatter
featuredImg
childImageSharp
fluid
...GatsbyImageSharpFluid
title
【讨论】:
哇!非常感谢,我真的应该看到的,但现在我明白了以上是关于查询返回错误“无法读取未定义的属性(读取'节点')”的主要内容,如果未能解决你的问题,请参考以下文章
得到以下错误 ORA-01427: 单行子查询返回多于一行 01427. 00000 - “单行子查询返回多于一行”