Node.js 如何对sequelize查询的每个结果进行API调用?
我有一个cron作业,它将查询我的数据库,并找到任何状态为“已提交”或“正在处理”的项目,我希望对每个项目调用外部API,然后用响应更新数据库Node.js 如何对sequelize查询的每个结果进行API调用?,node.js,api,cron,sequelize.js,Node.js,Api,Cron,Sequelize.js,我有一个cron作业,它将查询我的数据库,并找到任何状态为“已提交”或“正在处理”的项目,我希望对每个项目调用外部API,然后用响应更新数据库 const results = await myTable.findAll({ where: { status: { [op.or]: [ 'SUBMITTED', 'PROCESSING' ], }, }, }) 我尝试使用forE
const results = await myTable.findAll({
where: {
status: {
[op.or]: [
'SUBMITTED',
'PROCESSING'
],
},
},
})
我尝试使用forEach,但出现了错误“TypeError:cannotreadproperty'Symbol(Symbol.iterator)'”of undefined”
使用Promise.all从外部api收集所有响应,然后更新数据库。将整个过程包装在一个异步块中
let api_call_array = results.map( item => makeAPICall(item.id));
const api_response = await Promise.all([api_call_array])
let update_db_item = api_response.map((response, index)=> {
return updateDBItemStatus(results[index].item.id, response)
})
const update_response = await Promise.all([update_db_item])
谢谢,results.map可以工作,但它可以比您的解决方案简单得多。results.map(item=>makeAPICall(item.claimId)。然后(response=>updateDBItemStatus(item.id,response),)是的,Micheal,它会更简单,为了使它更高效,我们可以使用它,因为它不会对每个id都阻塞,您可以找到更高效的一个并使用它。
let api_call_array = results.map( item => makeAPICall(item.id));
const api_response = await Promise.all([api_call_array])
let update_db_item = api_response.map((response, index)=> {
return updateDBItemStatus(results[index].item.id, response)
})
const update_response = await Promise.all([update_db_item])