【问题标题】:Error while creating asset with Contentful Management SDK使用 Contentful Management SDK 创建资产时出错
【发布时间】:2020-04-14 12:20:06
【问题描述】:

我正在使用内容管理 JS SDK,版本 5.21.1

尝试创建新的图片资源,但出现错误。我尝试先上传图片并调用 createAsset:

const space = await this.client.getSpace(process.env.CONTENTFUL_SPACE_ID);
const environment = await space.getEnvironment(this.environment);
const upload = await environment.createUpload({ file: bytes });
const asset = await environment.createAsset({
  fields: {
    title: {
      [this.locale]: title
    },
    description: {
      [this.locale]: description
    },
    file: {
      [this.locale]: {
        fileName: fileName,
        contentType: contentType,
        uploadFrom: {
          sys: {
            type: 'Link',
            linkType: 'Upload',
            id: upload.sys.id
          }
        }
      }
    }
  }
});

asset.processForAllLocales();

return await asset.publish();

我也尝试过直接使用 createAssetFromFiles:

const space = await this.client.getSpace(process.env.CONTENTFUL_SPACE_ID);
const environment = await space.getEnvironment(this.environment);
const asset = await environment.createAssetFromFiles({
  fields: {
    title: {
      [this.locale]: title
    },
    description: {
      [this.locale]: description
    },
    file: {
      [this.locale]: {
        fileName: fileName,
        contentType: contentType,
        file: bytes
      }
    }
  }
});

asset.processForAllLocales();

return await asset.publish();

这是我得到的错误(两次调用都一样):

{
  "status": 422,
  "statusText": "Unprocessable Entity",
  "message": "Validation error",
  "details": {
    "errors": [
      {
        "name": "required",
        "path": [
          "fields",
          "file",
          "en-US",
          "url"
        ],
        "details": "The property \"url\" is required here"
      }
    ]
  },
  "request": {
    "url": "assets/01Ft5vBdTHzJPzIVJdBOlE/published",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/vnd.contentful.management.v1+json",
      "X-Contentful-User-Agent": "sdk contentful-management.js/5.21.1; platform node.js/v13.12.0; os macOS/19.4.0;",
      "Authorization": "Bearer ...",
      "user-agent": "node.js/v13.12.0",
      "Accept-Encoding": "gzip",
      "X-Contentful-Version": 1
    },
    "method": "put",
    "payloadData": null
  },
  "requestId": "07778ff81872ddb45d5e1a7266436e22"
}

通过阅读您的文档,我的印象是 URL 是在创建资产后自动创建的,所以我不确定这个错误是什么意思。

非常感谢任何帮助!

【问题讨论】:

    标签: contentful contentful-management contentful-api


    【解决方案1】:

    于是找到了根本原因:

    asset.processForAllLocales();
    
    return await asset.publish();
    

    我需要对已处理的资产调用发布:

    const processedAsset = asset.processForAllLocales();
    
    return await processedAsset.publish();
    

    不幸的是,SDK 错误消息很神秘,文档也很不完整。

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多