0

我正在使用内容管理 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,所以我不确定这个错误是什么意思。

任何帮助是极大的赞赏!

4

1 回答 1

1

于是找到了根本原因:

asset.processForAllLocales();

return await asset.publish();

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

const processedAsset = asset.processForAllLocales();

return await processedAsset.publish();

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

于 2020-04-29T20:56:16.827 回答