使用Contentful Management SDK创建资产时出错

时间:2020-04-14 12:20:06

标签: contentful contentful-management contentful-api

我正在使用Content Management 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,所以我不确定该错误是什么意思。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

因此找到了根本原因:

asset.processForAllLocales();

return await asset.publish();

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

const processedAsset = asset.processForAllLocales();

return await processedAsset.publish();

不幸的是,SDK错误消息是含糊不清的,文档非常不完整。