Gatsby: createNodeField having no effect

Created on 29 Jan 2018  路  9Comments  路  Source: gatsbyjs/gatsby

Description

I am using onCreateNode and createNodeField as I have seen referenced in many places, but node fields are not being added.

Environment

Gatsby version: 1.9.54
Node.js version: 8.8.1
Operating System: MacOS High Sierra

File contents (if changed):

In my gatsby-node.js:

exports.onCreateNode = ({
  node, boundActionCreators,
}) => {
  const { createNodeField } = boundActionCreators

  createNodeField({
    node,
    name: 'shareLink',
    value: 'arbitrary string'
  })
}

I have tried limiting the code to check for specific node.internal.type, as well as removed the .cache folder as referenced in other issues.

Actual result

"shareLink" field is not added. It is not discoverable in the graphql debugger and throws errors when the site attempts to access the field.

Expected behavior

"shareLink" field should exist in page nodes.

Surely I must be doing something wrong, but that would baffle me, given how many examples I have referenced for this functionality.

question or discussion

Most helpful comment

Curious where you were looking? Would love a PR from you to clarify that part of the docs!

All 9 comments

Did you look under fields? The new field should be at node.fields.newField.

Arg. That's the issue. Apologies, I didn't find that referenced!

Curious where you were looking? Would love a PR from you to clarify that part of the docs!

I'm having this exact issue and I have looked under node.fields which does not seem to exist.
I can only see the field added if I console out that node inside of onCreateNode just after calling actions.createNodeField

exports.onCreateNode = ({ node, actions, getNode }) => {
  if (!isLeverNode(node)) return;
  actions.createNodeField({
    node,
    name: 'slug',
    value: `${node.lever_id}_test`,
  });
};

I am having the same issue as @iDVB

I'm experiencing this same issue that @iDVB and @liltechnomancer is describing. Meaning, it shows up as node.fields.<name_of_field> inside of the onCreateNode function but not in a subsequent GQL query. If it's of any use, I'm trying to extend a node of the type Mdx.

Any insights?

Same here, using gatsby-source-prismic plugin

If I remember correctly, my issue was that I was running some async calls which I never made the gatsby API aware of(like a missing await or something like that). This is vague in my memory so take it with a grain of salt, but I recall my error being flaky and not consistent. https://www.gatsbyjs.org/docs/debugging-async-lifecycles/ was helpful anyways.

If I remember correctly, my issue was that I was running some async calls which I never made the gatsby API aware of(like a missing await or something like that). This is vague in my memory so take it with a grain of salt, but I recall my error being flaky and not consistent. https://www.gatsbyjs.org/docs/debugging-async-lifecycles/ was helpful anyways.

Sadly, this does not seem to be the issue, or it is beyond my understanding of the inner workings of Gatsby and I have no clue how to deal with that anyway. .
When I call createNodeField without any conditions, it appeares on gatsby nodes, that are not set up with the plugin. The plugin itself keeps on ignoring any attempt to add anything.

exports.onCreateNode = ({ node, actions}) => { const { createNodeField } = actions; // this adds x at some nodes, like allSitePage createNodeField({ node, name:x, value:x, }); // this is nowhere to be found.. if (node.internal.type ===PrismicBlogpost) { createNodeField({ node, name:slug, value:/blog/${node.uid}, }); console.log(actions, node) } };

but this seems to be more of gatsby-source-prismic issue, so I it does not belong in here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jimfilippou picture jimfilippou  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

magicly picture magicly  路  3Comments

kalinchernev picture kalinchernev  路  3Comments

Oppenheimer1 picture Oppenheimer1  路  3Comments