reactjs - 从 Netlify CMS 获取数据到 Gatsby JS
问题描述
我正在使用 Netlify CMS + Gatsby 创建一个站点。我已经在 Netlify 中拥有了站点结构和一些数据,但是在将数据导入 Gatsby 时遇到了一些问题。这是我到目前为止所拥有的
import React from 'react'
export default class IndexPage extends React.Component {
render({data}) {
const { edges: posts } = data.allMarkdownRemark
return (
<section className="section">
<div className="container">
<div className="content">
<h1 className="has-text-weight-bold is-size-2"></h1>
</div>
</div>
</section>
)
}
}
export const pageQuery = graphql`
query IndexQuery {
allMarkdownRemark(filter: {frontmatter: {templateKey: {regex: "/home-page|media-page/"}}}) {
edges {
node {
excerpt(pruneLength: 400)
id
fields {
slug
}
frontmatter {
title
templateKey
}
}
}
}
}
`
我得到 TypeError: Cannot read property 'data' of undefined
解决方案
不会作为props
参数传递到渲染中。
尝试:
import React from 'react'
export default class IndexPage extends React.Component {
render() {
const { edges: posts } = this.props.data.allMarkdownRemark
return (
<section className="section">
<div className="container">
<div className="content">
<h1 className="has-text-weight-bold is-size-2"></h1>
</div>
</div>
</section>
)
}
}
export const pageQuery = graphql`
query IndexQuery {
allMarkdownRemark(filter: {frontmatter: {templateKey: {regex: "/home-page|media-page/"}}}) {
edges {
node {
excerpt(pruneLength: 400)
id
fields {
slug
}
frontmatter {
title
templateKey
}
}
}
}
}
`
推荐阅读
- c# - 通过查找的实体引用
- python - ttk.Notebook 每个选项卡上的不同网格大小
- javascript - 带有数组包含的 Firestore where 语句不起作用
- python - sklearn 错误 ValueError:输入包含 NaN、无穷大或对于 dtype('float32')来说太大的值
- macos - 将应用程序放在前面
- django - Django 中的临时用户
- asynchronous - Flutter 计算方法冻结 UI
- excel - 将 Word 文档中的超链接链接到 Excel 工作表中的相应文档
- mysql - MySQL 中 FROM 子句中的子查询失败
- javascript - 如何在隐藏字段中保存单选按钮单击的值?