反应组件中的 gatsby staticquery 不返回任何数据

gatsby staticquery in a react component not returing any data

React 组件中的静态查询似乎运气不佳。似乎在页面上运行良好,但在组件上运行不佳。基本上 {data} 总是以未定义的形式出现。使用不同的 Graphql 代码片段尝试了 allMarkdownRemark 和 markdownRemark。当直接通过 graphql 客户端而不是通过反应组件查询时,查询返回正确的数据。

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>    import React from "react";
    import { StaticQuery, graphql } from "gatsby";
    import { Link } from "gatsby";
    import logo from "../../assets/images/logo.png";
    import footerMap from "../../assets/images/footer-map.png";

    export default () => {       
      return (
        <StaticQuery
          query={graphql`query FooterData {
      allMarkdownRemark(filter: {frontmatter: {componentKey: {eq: "Footer"}}}) {
        edges {
          node {
            frontmatter {
              explore {
                footerlinks1
                footerlinks1Url
              }
              headoffice {
                Address
                HOAddress
                HONumber
              }
              services {
                footerlinks2
                footerlinks2Url
              }
            }
          }
        }
      }
    }
    `}
     render={(data) => (
            <footer className="footer-area bg-color">
              <div className="container">
                <div className="row">
                  <div className="col-lg-4 col-sm-6">
                   <h3>Explore</h3>

                      <ul className="footer-links-list">
                      {
                        data.allMarkdownRemarks.edges[0].node.frontmatter.explore.map(link => (
                        <li>
                            <Link to={link.footerlinks1Url}>{link.footerlinks1}</Link>          
                        </li>
                        ))}
                        </ul>
                   </div>
               </div>
             </div>
           </footer>
       )}
       />
    )};
                  
Error I am getting is below. =========================== TypeError: Cannot read property 'edges' of undefined render C:/Users/HP/source/XXX/src/components/App/Footer.js:102 99 | <div className="single-footer-widget pl-5"> 100 | <h3>Explore</h3> 101 | > 102 | <ul className="footer-links-list"> 103 | { 104 | data.allMarkdownRemarks.edges[0].node.frontmatter.explore.map(link => ( 105 | <li> View compiled StaticQueryDataRenderer C:/Users/HP/source/XXX/.cache/gatsby-browser-entry.js:37 34 | : combinedStaticQueryData[query] && combinedStaticQueryData[query].data 35 | 36 | return ( > 37 | <React.Fragment> 38 | {finalData && render(finalData)} 39 | {!finalData && <div>Loading (StaticQuery)</div>} 40 | </React.Fragment> View compiled ▶ 19 stack frames were collapsed. This screen is visible only in development. It will not appear if the app crashes in production. Open your browser’s developer console to further inspect this error.

您的嵌套查询对象中有错字,您使用的是 allMarkdownRemarks(注意最后一个 S)而不是 allMarkdownRemark 将其替换为:

{data.allMarkdownRemark.edges[0].node.frontmatter.explore.map(link => (
  <li>
       <Link to={link.footerlinks1Url}>{link.footerlinks1}</Link>          
  </li>
))}