node.js - 如何对sequelize 查询的每个结果进行API 调用?

标签 node.js api cron sequelize.js

我有一个 cron 作业,它将查询我的数据库并查找状态为“已提交”或“正在处理”的任何项目,对于每个项目,我想对外部 API 进行 API 调用,然后使用响应更新数据库

  const results = await myTable.findAll({
    where: {
      status: {
        [op.or]: [
          'SUBMITTED',
          'PROCESSING'
        ],
      },
    },
  })

我尝试使用 forEach 但出现错误“TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined”

  await Promise.all(
        results.forEach(async item => {
          makeAPICall(item.id).then(response => updateDBItemStatus(item.id, response)
        }),
      )

最佳答案

使用 Promise.all 从外部 api 收集所有响应,然后更新数据库。将整个事情包装在一个异步 block 中。

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])

关于node.js - 如何对sequelize 查询的每个结果进行API 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550573/

相关文章:

linux - 如何更改 cron 输出电子邮件以获取主题上的唯一消息?

Cron 作业在 MySQL 表之间移动多行

node.js - VSCode : Prettier does not work with Dart Flutter

node.js - 如何使用 PhpStorm/WebStorm 连接到远程 Nodejs 调试 session ?

javascript - 客户端无法使用 socket.io 连接到服务器

c# - 在 C# 中通过 Imgur API 匿名获取图像信息

javascript - 使用javascript fetch将数据发布到django rest框架

winapi - 枚举子项中的所有值

node.js - 如何使用node.js的ffi将输入键发送到窗口

Cronjobs 接踵而至