【发布时间】:2023-02-10 01:53:05
【问题描述】:
我正在尝试使用 useStaticQuery 从 GraphQL Gatsby 获取信息,但返回的数据是 undefined,我不明白为什么,因为在我的 http://localhost:8000/___graphql 中我收到了很好的信息。
我的代码不是页面组件,这是我使用Static Query的原因
我的代码是这样的:
import React from "react";
import { useStaticQuery, graphql} from "gatsby";
export default function MenuMD () {
const { data } = useStaticQuery(
graphql`
query {
allFile(filter: {sourceInstanceName: {eq: "markdown"}}) {
edges {
node {
childrenMarkdownRemark {
frontmatter {
slug
title
}
}
}
}
}
}
`
)
console.log('static data', data);
return<>Menu from MarkDown</>
}
http://localhost:8000/___graphql 的预期结果是这样的:
{
"data": {
"allFile": {
"edges": [
{
"node": {
"childMarkdownRemark": {
"frontmatter": {
"slug": "/projet_m",
"title": "Projet M"
}
}
}
},
{
"node": {
"childMarkdownRemark": {
"frontmatter": {
"slug": "/projet_n",
"title": "Projet N"
}
}
}
}
]
}
},
"extensions": {}
}
undefined 返回可能有原因吗?
【问题讨论】:
标签: javascript reactjs graphql gatsby