Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR: Cannot read property 'replace' of undefined, onCreateNode, while using contentful cms #20560

Closed
AnnieTaylorCHEN opened this issue Jan 12, 2020 · 3 comments
Labels
status: needs more info Needs triaging and reproducible examples or more information to be resolved status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting.

Comments

@AnnieTaylorCHEN
Copy link
Contributor

AnnieTaylorCHEN commented Jan 12, 2020

Description

Error msg:


 ERROR #11321  PLUGIN

"gatsby-node.js" threw an error while running the onCreateNode lifecycle:

Cannot read property 'replace' of undefined



  TypeError: Cannot read property 'replace' of undefined
  
  - path.js:54 slash
    [PetiteMinimal]/[gatsby-core-utils]/dist/path.js:54:15
  
  - create-file-path.js:41 module.exports
    [PetiteMinimal]/[gatsby-source-filesystem]/create-file-path.js:41:61
  
  - gatsby-node.js:16 Object.exports.onCreateNode
    /home/annie/Desktop/PetiteMinimal/gatsby-node.js:16:18
  
  - api-runner-node.js:235 runAPI
    [PetiteMinimal]/[gatsby]/dist/utils/api-runner-node.js:235:41
  
  - api-runner-node.js:375 Promise.catch.pluginName
    [PetiteMinimal]/[gatsby]/dist/utils/api-runner-node.js:375:13
  
  - new Promise
  
  - api-runner-node.js:374 runPlugin
    [PetiteMinimal]/[gatsby]/dist/utils/api-runner-node.js:374:10
  
  - api-runner-node.js:328 module.exports
    [PetiteMinimal]/[gatsby]/dist/utils/api-runner-node.js:328:24
  
  - task_queues.js:93 processTicksAndRejections
    internal/process/task_queues.js:93:5

I suspect that the errors are caused by onCreateNode in gatsby-node.js file. Because for some reasons I have 49 totalCount in allMarkdownRemark, while I only have 6 md files to create blog posts. Those extra files might come from contentful cms for some reason, they have sourceName and absolutepath as Null, but they do have internal.content, which are some filler text, which appear in some of my contentful data.

When I commented this out, together with blog templates and createPages for blog files, I no longer had the error msg. That's why I suspect it was caused by the those extra files (that is not from my md files) with a lot null fields.

Reference link:
Click here

Steps to reproduce

Almost impossible to reproduce unless you have exactly the same content as my contentful cms.

Expected result

can deploy to netlify.

Actual result

when I run gatsby develop, site is working correctly locally as expected despite the console has errors. And those errors are causing gatsby build to fail, so I can't deploy it.

Environment

System:
OS: Linux 5.0 Linux Mint 19.3 (Tricia)
CPU: (4) x64 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
Shell: 4.4.20 - /bin/bash
Binaries:
Node: 12.13.0 - ~/.nvm/versions/node/v12.13.0/bin/node
npm: 6.12.0 - ~/.nvm/versions/node/v12.13.0/bin/npm
Languages:
Python: 2.7.17 - /usr/bin/python
Browsers:
Chrome: 79.0.3945.117
Firefox: 72.0.1
npmPackages:
gatsby: ^2.18.8 => 2.18.11
gatsby-image: ^2.2.34 => 2.2.36
gatsby-plugin-commercelayer: ^1.2.10 => 1.2.10
gatsby-plugin-manifest: ^2.2.31 => 2.2.33
gatsby-plugin-offline: ^3.0.27 => 3.0.29
gatsby-plugin-react-helmet: ^3.1.16 => 3.1.18
gatsby-plugin-sharp: ^2.3.5 => 2.3.7
gatsby-plugin-snipcart: ^1.1.2 => 1.1.2
gatsby-remark-images: ^3.1.38 => 3.1.38
gatsby-remark-source-name: ^1.0.0 => 1.0.0
gatsby-source-contentful: ^2.1.76 => 2.1.76
gatsby-source-filesystem: ^2.1.40 => 2.1.42
gatsby-transformer-remark: ^2.6.42 => 2.6.42
gatsby-transformer-sharp: ^2.3.7 => 2.3.9
npmGlobalPackages:
gatsby-cli: 2.8.26

@AnnieTaylorCHEN
Copy link
Contributor Author

According to #20398, it seems all the longText from contentful is markdown. This might explains why I have those extra files.

Then.... what should I do with onCreateNode to generate md files for blog posts only?

@LekoArts LekoArts added status: needs more info Needs triaging and reproducible examples or more information to be resolved status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. labels Jan 13, 2020
@LekoArts
Copy link
Contributor

LekoArts commented Jan 13, 2020

Hi!

Sorry to hear you're running into an issue. To help us best begin debugging the underlying cause, it is incredibly helpful if you're able to create a minimal reproduction. I know that you linked a repository but unfortunately we don't have access to your contentful space. Creating such an example will also help you see at which point the problem occurs.

Then.... what should I do with onCreateNode to generate md files for blog posts only?

You filter for the sourceInstanceName namely blog:

exports.onCreateNode = ({ node, getNode, ...rest }) => {
  const fileNode = getNode(node.parent)
  const source = fileNode.sourceInstanceName

  if (node.internal.type === `MarkdownRemark` && source === 'blog') {}
}

@AnnieTaylorCHEN
Copy link
Contributor Author

Thank you so much Lennart @LekoArts .

I know a minimal production would be nicer... I will try that next time.

I also tried your code but it resulted in errors which is similar to the above. However, I tried those myself and finally was able to run gatsby build and deployed my site. For future reference here is my code:

exports.onCreateNode = ({ node, getNode, actions }) => {
  const { createNodeField } = actions
  
    if (node.internal.type === "MarkdownRemark" && node.fields.sourceName
     === "blog") {
      const slug = createFilePath({ node, getNode, basePath: "posts" })
      console.log('slug :', slug)
      createNodeField({
        node,
        name: "slug",
        value: slug
      })
    }
    
  } 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs more info Needs triaging and reproducible examples or more information to be resolved status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting.
Projects
None yet
Development

No branches or pull requests

2 participants