javascriptcontentfulcontentful-management

TypeError: migrationCreator is not a function


I am trying to createAsset using the content-management-api.

The JavaScript script that I am using is

./contentful/contentful-import.js

const contentful = require('contentful-management');

const client = contentful.createClient({
  accessToken:'AUTHTOKEN'
});

client
  .getSpace('SPACE')
  .then(space => {
    space.createAsset({
      fields: {
        title: {
          'en-US': 'Example 1'
        },
        description: {
          'en-US': 'Example Description'
        },
        file: {
          'en-US': {
            contentType: 'image/jpeg',
            fileName: 'example1.jpeg',
            upload:'https://example1.jpeg'
          }
        }
      }
    }),
    space.createAsset({
      fields: {
        title: {
          'en-US': 'Example 2'
        },
        description: {
          'en-US': 'Example Description'
        },
        file: {
          'en-US': {
            contentType: 'image/jpeg',
            fileName: 'example2.jpeg',
            upload:'https://example2.jpeg'
          }
        }
      }
    }),
    //... 700 other assets
  })
  .then(asset => asset.processForAllLocales())
  .then(asset => console.log(asset))
  .catch(console.error)

And the CLI function I run is

contentful space migration ./contentful/contentful-import.js

Which is returning the error of TypeError: migrationCreator is not a function

I have looked elsewhere in the Contentful documentation but I cannot see anything that helps.

Am I attempting to upload assets correctly or have I done something wrong?


Solution

  • The script you have written is a "normal" node.js script.

    node contentful-import.js
    

    Will do the job just fine for that. contentful space migration uses specific scripts in a particular format (they have to export a migrationCreator). A valid migration would be:

    module.exports = function (migration, context) {
      const dog = migration.createContentType('dog');
      const name = dog.createField('name');
      name.type('Symbol').required(true);
    };
    

    The contentful-cli uses contentful-migration under the hood. You'll find more documentation there.

    I'll open a PR for the CLI documentation right away. :)