Contentful logo

Contentful Community

Solved: 'ls-content-as-code'Content Migration Examples - TypeError: Cannot read property 'split' of undefined

Hi,

I’ve been trying to follow ‘ls-content-as-code’ Contentful Content Migration Examples downloaded from here and I only managed to:

  • import blog-post-example-.json using:
    contentful space import --content-file ./contentful-exports/blog-post-example.json

  • create new ‘author’ content type using:
    contentful space migration --environment-id dev 01-add-author-content-type.js

Unfortunately when I try to run the second migration script (02-link-author.js) the migration is unsuccessful.

Problematic script:

module.exports = function (migration) {

    const blogPost = migration.editContentType('blogPost');

    blogPost.createField('blogAuthor')
        .name('Blog author')
        .type('Link')
        .linkType('Entry')
        .validations([
            {
                "linkContentType": ['author']
            }
        ]);

    blogPost.moveField('blogAuthor').afterField('title');


// Derives author first name and last name based on the existing author field, and link these back to blog post entries.
migration.deriveLinkedEntries({
    // Start from blog post's author field
    contentType: 'blogPost',
    from: ['author', 'title'],
    // This is the field we created above, which will hold the link to the derived category entries.
    toReferenceField: 'blogAuthor',
    // The new entries to create are of type 'name', 'firstName' and 'lastName'.
    derivedContentType: 'author',

    derivedFields: ['name', 'firstName', 'lastName'],
    identityKey: async (from) => {
        // The blog post title will be used as an identity key.
        return from.title['en-US'].toLowerCase().replace(/\s+/g, '-');
    },
    deriveEntryForLocale: async (from, locale) => {

        const name = from.author[locale];
        const [firstName, lastName] = from.author[locale].split(' ');
        return {
            name,
            firstName,
            lastName
        }
    }
})

// Disable the old field for now so editors will not see it.
blogPost.editField('author').disabled(true)
}

Terminal output:

Update Content Type blogPost

  Create field blogAuthor
    - name: "Blog author"
    - type: "Link"
    - linkType: "Entry"
    - validations: [{"linkContentType":["author"]}]

  Move field blogAuthor after field title

0 errors while transforming this content. Please check the errors log for details: C:\Users\*****\Projects\ls-content-as-code\errors-02-author-link-1635235112109.log

🚨  Migration unsuccessful
Derive entries from blogPost
  - from: author,title
  - to: name,firstName,lastName
  - via: blogAuthor

1 errors while transforming this content. Please check the errors log for details: C:\Users\*****\Projects\ls-content-as-code\errors-02-author-link-1635235112109.log

🚨  Migration unsuccessful
Update Content Type blogPost

  Update field author
    - disabled: true

0 errors while transforming this content. Please check the errors log for details: C:\Users\*****\Projects\ls-content-as-code\errors-02-author-link-1635235112109.log

🚨  Migration unsuccessful
  Error: Runtime Errors
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ Error: Runtime Errors                                                                                                                                                                                                            ││     at run (C:\Users\*****\AppData\Roaming\npm\node_modules\contentful-cli\node_modules\contentful-migration\built\bin\cli.js:146:19)                                                                                       │└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

Error log file:

TypeError: Cannot read property 'split' of undefined

    at EntryDeriveAction.deriveEntryForLocale (C:\Users\Jankowskim\Projects\ls-content-as-code\02-author-link.js:88:69)

    at processTicksAndRejections (internal/process/task_queues.js:95:5)

    at async EntryDeriveAction.applyTo (C:\Users\*****\AppData\Roaming\npm\node_modules\contentful-cli\node_modules\contentful-migration\built\lib\action\entry-derive.js:54:47)

    at async IntentList.applyTo (C:\Users\*****\AppData\Roaming\npm\node_modules\contentful-cli\node_modules\contentful-migration\built\lib\intent-list\index.js:87:21)

    at async migration (C:\Users\*****\AppData\Roaming\npm\node_modules\contentful-cli\node_modules\contentful-migration\built\lib\migration-parser.js:148:9)

    at async run (C:\Users\*****\AppData\Roaming\npm\node_modules\contentful-cli\node_modules\contentful-migration\built\bin\cli.js:114:23)

Oddly, when I console.logged ‘from.author[locale].split()’ from the line in question it printed correct values instead of ‘undefined’. All variables also get assigned correct vallues.

  deriveEntryForLocale: async (from, locale) => {

            const name = from.author[locale];
            const [firstName, lastName] = from.author[locale].split(' ');

            // Line below produces: Lisa Bibko Lisa Bibko Lisa Bibko (correct values).
            console.log(from.author[locale].split(), name, firstName, lastName);
            return {
                name,
                firstName,
                lastName
            }
        }

I should add that I’m behind a VPN proxy , which I passed to Contentful CLI config with:

contentful config add --proxy

However, given the fact that the first two commands (import and creating new ‘author’ content type) were successful it’s unlikely to be caused by proxy or authentication issues.

Can anyone suggest what might be causing the problem?

SOLVED:

I was doing the above example using an existing space of mine. I imported provided content and content type into my own space, that I had been experimenting with before. As part of that experimentation I created an additional locale, which I forgot about. Method deriveEntryForLocale() iterates over existing locales. When it ran against my custom locale it encountered ‘undefined’.