question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Cannot read property 'forEach' of undefined

See original GitHub issue

EDIT: I debugged this issue, see my comment below.


I’ve read up on how to use Contentful migrations and wanted to try it out for the very first time. However, when I was running my migration script I got the error Cannot read property 'forEach' of undefined:

🚨  Migration unsuccessful
Unexpected token C in JSON at position 0

🚨  Error: Cannot read property 'forEach' of undefined

  TypeError: Cannot read property 'forEach' of undefined
      at run ([…]/node_modules/contentful-migration/built/bin/cli.js:166:24)
      at processTicksAndRejections (internal/process/task_queues.js:94:5)

The reason why I’m creating an issue here is because I believe that this error is in fact not coming from my migration script. According to the stack trace the error is coming from here: https://github.com/contentful/contentful-migration/blob/be850708122f9cdd5a132d75711fa278bd0ca543/src/bin/cli.ts#L187

This issue seems to be related to https://github.com/contentful/contentful-migration/issues/183. However, #183 was solved by using a different technology (the python content management SDK) to accomplish the goal. I’d rather want this tool here to work though.

Expected Behavior

Either a successful migration or an error message telling what exactly was wrong with my migration script.

Actual Behavior

Inactionable error message / error within the tool itself.

Screenshot 2020-02-25 at 17 37 56

Possible Solution

Either this is a bug in the contentful-migration CLI that needs to be fixed or the error messages should become more helpful / actionable.

Steps to Reproduce

I don’t want to publicly post our space ID, but also don’t want to go through the effort of creating a separate repo for showcasing the error. So, I’ll just post the migration script in case this is of any help:

// migrations/01-lowercase-slug.js

module.exports = function(migration) {
  const transformEntryForLocale = fromFields => {
    const prevSlug = fromFields.slug
    const newSlug =
      typeof prevSlug === 'string' ? prevSlug.toLowerCase() : prevSlug
    return { slug: newSlug }
  }

  migration.transformEntries({
    contentType: 'crmPage',
    from: ['slug'],
    to: ['slug'],
    shouldPublish: 'preserve',
    transformEntryForLocale,
  })

  migration.transformEntries({
    contentType: 'pcmPage',
    from: ['slug'],
    to: ['slug'],
    shouldPublish: 'preserve',
    transformEntryForLocale,
  })
}

Environment

  • Node Version: v12.14.1
  • Package Manager Version: yarn 1.21.1
  • Operating System: Darwin Olivers-MBP.fritz.box 19.2.0 Darwin Kernel Version 19.2.0: Sat Nov 9 03:47:04 PST 2019; root:xnu-6153.61.1~20/RELEASE_X86_64 x86_64
  • Package Version: 1.2.13 (contentful -v)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
phoebeschmidtcommented, Mar 5, 2020

🎉 This issue has been resolved in version 1.0.13 🎉

The release is available on:

Your semantic-release bot 📦🚀

0reactions
osartuncommented, Apr 14, 2020

@patrickdmccarthy So, my way of debugging this might not be the most elegant way: I went into my node_modules directory (!), searched for the contentful-migration folder and added some console.logs to it. Later, I also added debugger statements or breakpoints in VSCode and attached the Node debugger to the process (See here: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_attach-to-node-process-action). The code in my node_modules folder is the built code which obviously looks different than the source code in the git repo here, so I looked into the source code here and compared it to the code I read in my node_modules folder to see where I’m at. The source code is easier to read and to understand, of course.

I hope this can help you! 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'forEach' of undefined - Stack Overflow
And undefined has no forEach method. To fix this, always make sure you put a semicolon at the end of your lines (or...
Read more >
[AskJS] Why does this simple forEach give error Cannot read ...
But the following code gives an error script.js?v=3:196 Uncaught TypeError: Cannot read property 'forEach' of undefined :
Read more >
TypeError: Cannot read property 'forEach' of undefined - General
TypeError : Cannot read property 'forEach' of undefined ... This topic was automatically closed 60 days after the last reply. New replies are...
Read more >
Cannot read properties of undefined (reading 'forEach')
Have you ever encountered this error in javascript before TypeError: Cannot read properties of undefined (reading 'forEach') there are many ...
Read more >
TypeError: Cannot read property 'forEach' of undefined
I was updating to the most recent SPT-AKI update and i had this error when the game launched, I have no idea what's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found