通过NodeJS脚本迭代数组中的项目

问题描述 投票:0回答:1

我正在尝试使用contentful-management API找到在Contentful中创建多个资产的解决方案。

用于实现资产的单个创建的nodeJS脚本是

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

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createAssetWithId('<asset_id>', {
  title: {
    'en-US': 'Example 1'
  },
  file: {
    'en-US': {
      contentType: 'image/jpeg',
      fileName: 'example1.jpeg',
      upload: 'https://example.com/example1.jpg'
    }
  }
}))
.then((asset) => asset.processForAllLocales())
.then((asset) => asset.publish())
.then((asset) => console.log(asset))
.catch(console.error)

这很简单,很容易实现。但是,当想要创建多个资产时,这不起作用。

经过几个小时寻找一种记录在案的方式来实现这一点,但没有成功,我来到了

const contentful = require('contentful-management');
const assets = require('./assetObject.js');

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

  const space = await client.getSpace('<space_id>');
  const environment = await space.getEnvironment('<environment-id>');
  const createdAssets = await Promise.all(
    assets.map(
      asset =>
        new Promise(async () => {
          let cmsAsset;

          try {
            cmsAsset = await environment.createAssetWithId(asset.postId, {
              fields: {
                title: {
                  'en-US': asset.title
                },
                description: {
                  'en-US': asset.description
                },
                file: {
                  'en-US': {
                    contentType: 'image/jpeg',
                    fileName: asset.filename,
                    upload: asset.link
                  }
                }
              }
            });
          } catch (e) {
            throw Error(e);
          }
          try {
            await cmsAsset.processForAllLocales();
          } catch (e) {
            throw Error(e);
          }
          try {
            await cmsAsset.publish();
          } catch (e) {
            throw Error(e);
          }
        })
    )
  );
  return createdAssets;
};

assetObject.js

[
 {
    link: 'https://example.com/example1.jpg',
    title: 'Example 1',
    description: 'Description of example 1',
    postId: '1234567890',
    filename: 'example1.jpeg'
  }, ... // Many more
]

这在运行时不会产生错误,也不会产生任何错误。我做错了什么?这是我应该使用的方法吗?

javascript node.js contentful contentful-management
1个回答
1
投票

新的承诺需要“解决”和“拒绝”,所以对我来说代码应该是这样的

 const createdAssets = await Promise.all(
    assets.map(
      asset =>
        new Promise(async (resolve, reject) => {    
          try {
            const cmsAsset = await environment.createAssetWithId(asset.postId, {
              fields: {
                title: {
                  'en-US': asset.title
                },
                description: {
                  'en-US': asset.description
                },
                file: {
                  'en-US': {
                    contentType: 'image/jpeg',
                    fileName: asset.filename,
                    upload: asset.link
                  }
                }
              }
            });
            await cmsAsset.processForAllLocales();
            await cmsAsset.publish();
            resolve(cmsAsset);
          } catch (e) {
            reject(e);
          }
        })
    )
  );
  return createdAssets;

跳它可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.