从 Gatsby 中删除节点
Remove nodes from Gatsby
我使用的插件可以根据 API 请求自动为我创建节点。它运行良好,但它 returns 的数据比我需要的多,包括与我的应用程序无关的节点。我在 gatsby-node
中的 onCreateNode
时如何删除节点?
例如。我只想拥有带标题的节点。如果它有标题,我想保留它,并添加一个字段。如果没有,我想删除它。这是正确识别节点类型:
if(node.internal.type === `community_education__classes` && node.title && node.title._t) {
const correctedClassObject = classCorrector(node.content._t);
createNodeField({
node,
name: `className`,
value: node.title._t,
});
}
所以我可以像这样找到我想删除的节点
if(node.internal.type === `community_education__classes` && (!node.title || !node.title._t)) {
// need code to delete node that matched these conditions
}
我希望有一个我找不到的 Gatsby API?
您可以使用 Gatsby 的 deleteNode
,它是 actions
fka boundActionCreators
.
的一部分
exports.onCreateNode = ({ node, boundActionCreators }) => {
const { deleteNode } = boundActionCreators;
// Check the node, delete if true.
if (condition) {
deleteNode(node);
}
}
我使用的插件可以根据 API 请求自动为我创建节点。它运行良好,但它 returns 的数据比我需要的多,包括与我的应用程序无关的节点。我在 gatsby-node
中的 onCreateNode
时如何删除节点?
例如。我只想拥有带标题的节点。如果它有标题,我想保留它,并添加一个字段。如果没有,我想删除它。这是正确识别节点类型:
if(node.internal.type === `community_education__classes` && node.title && node.title._t) {
const correctedClassObject = classCorrector(node.content._t);
createNodeField({
node,
name: `className`,
value: node.title._t,
});
}
所以我可以像这样找到我想删除的节点
if(node.internal.type === `community_education__classes` && (!node.title || !node.title._t)) {
// need code to delete node that matched these conditions
}
我希望有一个我找不到的 Gatsby API?
您可以使用 Gatsby 的 deleteNode
,它是 actions
fka boundActionCreators
.
exports.onCreateNode = ({ node, boundActionCreators }) => {
const { deleteNode } = boundActionCreators;
// Check the node, delete if true.
if (condition) {
deleteNode(node);
}
}