Error: Cannot read property 'forEach' of undefined
See original GitHub issueEDIT: 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.
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:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
🎉 This issue has been resolved in version 1.0.13 🎉
The release is available on:
Your semantic-release bot 📦🚀
@patrickdmccarthy So, my way of debugging this might not be the most elegant way: I went into my
node_modules
directory (!), searched for thecontentful-migration
folder and added someconsole.log
s to it. Later, I also addeddebugger
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! 🙏