rest - gatsby graqpql query rest API 只返回一个结果
问题描述
我已经开始使用 gatsby 和 graphQL,并试图从 rest API 中获取 dat。我能够遍历数据并将其登录到控制台。但是,当使用 graphQL 时,我只得到一个结果。请在下面查看我的代码。我感谢任何帮助我做错了什么。
exports.sourceNodes = async ({ boundActionCreators }) => {
const { createNode } = boundActionCreators;
console.log(createNode)
const fetchData = () => axios.get(url)
const res = await fetchData();
console.log(res.data);
res.data.blogArticle.forEach((article, index) => {
return createNode({
...article,
id: `£{index}`,
children: [],
parent: `__SOURCE__`,
internal: {
type: `BlogArticle`,
content: JSON.stringify(article),
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(article))
.digest(`hex`)
},
heading: article.heading,
subheading: article.subheading,
content: {
text: article.content.text,
videoURL: article.content.videoURL,
imageURL: article.content.imageURL
}
});
});
res.data.emptyLegs.forEach((leg, index) => {
const emptyLegNode = {
from: leg.From,
to: leg.To,
aircraft: leg.Aircraft,
seats: leg.Seats,
date: leg.Date,
price: leg.Price,
id: `£{index}`,
children: [],
parent: `__SOURCE__`,
internal: {
type: `EmptyLegs`,
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(res.data.emptyLegs))
.digest(`hex`)
},
};
createNode(emptyLegNode);
});
return;
};
解决方案
我认为一个简单的错字导致了问题。
改变:
return createNode({ id: `£{index}`,
至:
return createNode({ id: `${index}`,
并且在空腿节点 id: 内相同${index}
,
这应该可以修复错误。
推荐阅读
- terraform - 一个空的“azurerm_subscription”数据块究竟如何获得它的数据源参数?
- html - 如何在引导程序中在列之间留出空间
- c - 我不太确定我的算法的大 O 是 O(n^3) 还是 O( m * n * n )
- javascript - 在 JS 中,有没有办法给一个对象的多个字段赋值,而不用每次都写对象的名字?
- ruby - Nginx 未知指令“auth_request”
- c# - vb.net 到 c# - 我们如何纠正这个错误
- swift - 属性“body”的返回类型需要“Box”
- performance - 与例如 Java 相比,F# 性能较差。我究竟做错了什么?
- python - Numba 和 llvmlite 不会安装在 sage cygwin 上
- r - 在 R 中的世界地图顶部绘制点,错误:提供连续比例的离散值