Your approach is partially correct. You are using a promise-based approach but when using then()
you are already settling and partially resolving it so you don't need to use the callback of resolve()
, which may be causing a duplication of the promise function so try removing it.
Additionally, you may want to use a more friendly approach using async
/await
functions. Something like:
exports.createPages = async ({ graphql, actions, reporter }) => {
const yourQuery= await graphql(
`
{
allWordpressPost {
edges{
node{
id
title
slug
excerpt
content
}
}
}
}
`
if (yourQuery.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const postTemplate = path.resolve("./src/templates/post.js")
_.each(yourQuery.data.allWordpressPost.edges, edge => {
createPage({
path: `/post/${edge.node.slug}/`,
component: slash(postTemplate),
context: edge.node,
})
})
})
// and so on for the rest of the queries
};
In addition, place a console.log(pageContext)
in your postTemplate
to get what's reaching that point and name the template as:
const Post = ({pageContext}) => {
console.log("your pageContext is", pageContext);
return <div>
<h1>
{pageContext.title}
</h1>
</div>
}
export default Post;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…