【发布时间】:2021-05-09 01:08:59
【问题描述】:
我正在从事一个 Gatsby 项目,我在 Graphql 中从 JSON 数据中查询,该数据在 Graph IU 中正确返回所有数据,我什至可以将其注销并在浏览器 devtools 中查看结果。但是由于某种原因,当我缩短我的 Graphql 字段以使用短名称时,只有部分数据正在通过,正如您将在下面的代码中看到的那样:
这里是静态查询组件:
import { useStaticQuery, graphql } from "gatsby"
export const UseFooterDataQuery = () => {
const allFooterData = useStaticQuery(graphql`
query FooterDataQuery {
allDataJson {
edges {
node {
id
title
url {
id
linkName
linkUrl
}
}
}
}
}
`)
return allFooterData
}
这是 Graphql IU 中相同查询的结果:
{
"data": {
"allDataJson": {
"edges": [
{
"node": {
"id": "8e2be307-9298-52b2-9a78-cf1a9dfa5b35",
"title": "Features",
"url": [
{
"id": "022",
"linkName": "Version Control",
"linkUrl": "version-control"
},
{
"id": "023",
"linkName": "Design Collaboration",
"linkUrl": "design-collaboration"
},
{
"id": "024",
"linkName": "Developer Handoff",
"linkUrl": "developer-handoff"
}
]
}
}
]
}
},
"extensions": {}
}
这是我的功能组件。看到我正在控制台记录 UseFooterDataQuery() 函数和返回 allFooterList 我只是缩短了 Graphql 字段名称:
import React from "react"
import { Link } from "gatsby"
import { UseFooterDataQuery } from "../hooks/UseFooterDataQuery"
export default function Footer() {
const getAllFooterList = () => {
const allFooterList = []
const allFooter = UseFooterDataQuery()
allFooter.allDataJson.edges.forEach(footerEdge => {
allFooterList.push({
id: footerEdge.node.id,
title: footerEdge.node.title,
linkName: footerEdge.node.url.linkName,
linkUrl: footerEdge.node.url.linkUrl,
linkId: footerEdge.node.url.id,
})
})
return allFooterList
}
const allFooterList = getAllFooterList()
console.log(UseFooterDataQuery())
console.log(allFooterList)
return (
<FooterSection>
<FooterContainer>
{allFooterList.map(data => {
return (
<div key={data.id}>
<h3>{data.title}</h3>
<ul>
<li>
<Link to={`/${data.linkUrl}`}>{data.linkName}</Link>
</li>
</ul>
</div>
)
})}
</FooterContainer>
<div>
© {new Date().getFullYear()}, Built with
{` `}
<a href="https://www.gatsbyjs.com">Gatsby</a>
</div>
</FooterSection>
)
}
这里返回上面的控制台日志。如您所见,UseFooterDataQuery() 函数返回了所有数据,但我无法弄清楚 allFooterList 的返回发生了什么,其中我的数据 linkId、linkName 和 linkUrl 未定义。
{…}
allDataJson: {…}
edges: (1) […]
0: {…}
node: {…}
id: "8e2be307-9298-52b2-9a78-cf1a9dfa5b35"
title: "Features"
url: (3) […]
0: Object { id: "022", linkName: "Version Control", linkUrl: "version-control" }
1: Object { id: "023", linkName: "Design Collaboration", linkUrl: "design-collaboration" }
2: Object { id: "024", linkName: "Developer Handoff", linkUrl: "developer-handoff" }
length: 3
<prototype>: Array []
<prototype>: Object { … }
<prototype>: Object { … }
length: 1
<prototype>: Array []
<prototype>: Object { … }
<prototype>: {…
allFooterList console.log 返回:
0: {…}
id: "8e2be307-9298-52b2-9a78-cf1a9dfa5b35"
linkId: undefined
linkName: undefined
linkUrl: undefined
title: "Features"
<prototype>: Object { … }
length: 1
<prototype>: [
【问题讨论】:
标签: javascript json reactjs graphql gatsby