Angular & Contentful: Cannot create asset inside a space

假装没事ソ 提交于 2019-12-24 19:47:26

问题


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:


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)


来源:https://stackoverflow.com/questions/50415770/angular-contentful-cannot-create-asset-inside-a-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!