在Node中的数组中的每个对象上调用一个函数

时间:2018-04-27 13:47:09

标签: node.js mongoose es6-promise

我正在尝试批量处理数据库更新以重新填充mongo集合。我创建了一个对象来保存从外部源查找数据所需的属性,然后将其添加回MongoDb集合。

数组如下所示:

const pops = [ 
    { table: 'SFAccounts',
      label: 'Account__c', 
      createListName: 'Accounts'
    },
    { table: 'SFTimes',
      label: 'CusTime__c', 
      createListName: 'Time'
    }]

我想创建一个带有'table','label和'createListName'的函数,它基本上就像这样......

async function processData(table, label, createListName) {
    // Get some info from Salesforce 
    const dataFromSF = await getMetaDataFromSalesForce(table)
    // Extract the parts I actually need
    const relevantBits = dataFromSF.filter(field => field.name === label)
    //Create a new list in the db
    const createResult = await List.create( { name: createListName, values: relevantBits } )
    return createResult
}

最终目标是达到类似

的目标
await Promise.all(processData(pops))

将等待所有被拉入并填充到数据库中的表。

1 个答案:

答案 0 :(得分:1)

如果更改processData的参数:

async function processData({table, label, createListName}) {
    // Get some info from Salesforce 
    const dataFromSF = await getMetaDataFromSalesForce(table)
    // Extract the parts I actually need
    const relevantBits = dataFromSF.filter(field => field.name === label)
    //Create a new list in the db
    const createResult = await List.create( { name: createListName, values: relevantBits } )
    return createResult
}

只是await Promise.all(pops.map(processData));