0
votes

I have an Angular application that uses Contentful (v. 5.1.3). I want to upload an asset to Contentful from the Angular app. According to the official site, the correct way to do it is:

client.getSpace('<space_id>')
.then((space) => space.createAsset({ ... })

See here: https://www.contentful.com/developers/docs/references/content-management-api/#/reference/assets

The problem is that the Space object does not have a createAsset method. In fact, the only methods it has are locales, name, sys and toPlainObject.

I installed Contentful with npm install contentful. Is there some kind of issue with the package? Is there a reason why the Space object has been stripped of all useful methods?

Thanks,

Alex

1

1 Answers

1
votes

You need to use the contentful-management.js library rather than the the contentful.js library in order to create assets. If you notice in the code sample on the link you provided, it is using the management library:

const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.createAsset({
  fields: {
    title: {
      'en-US': 'Playsam Streamliner'
    },
    description: {
      'en-US': 'Streamliner description'
    },
    file: {
      'en-US': {
        contentType: 'image/jpeg',
        fileName: 'example.jpeg',
        upload: 'https://example.com/example.jpg'
      }
    }
  }
}))
.then((asset) => asset.processForAllLocales())
.then((asset) => console.log(asset))
.catch(console.error)