使用 forEach 和 axios 进行异步/等待

Oya*_*abi 0 javascript node.js async-await axios

我遇到了一个问题,需要你的帮助。我需要在名为 rudder 的软件中使用自动注册服务器的 API。为了达到我的目标,我必须使用 2 个 http 请求:

  1. (GET) 获取所有处于挂起状态的服务器
  2. (POST)注册我的服务器

我的第一个请求得到了完美处理,但我不知道如何实现第二个请求。

这是我的代码(/src/controllers/registration.js):

async function index(req, res) {
  // Check parameter
  if (!req.body.hostname) {
    return res.status(422).json({ result: 'error', message: 'Missing hostname parameter' });
  }

  try {
    const pendingNodes = await axios.get(`${config.rudderURL}/rudder/api/latest/nodes/pending?include=minimal`, { headers });
    Object.keys(pendingNodes.data.data.nodes).forEach((key) => {
      // Search in all pending machine if our node is present
      const node = pendingNodes.data.data.nodes[key];
      if (req.body.hostname === node.hostname) {
        // Registration
        const response = async axios.get(`${config.rudderURL}/rudder/api/nodes/pending/${node.id}`, { headers });
        // console.log(response);
        return res.status(200).json({ result: 'success', message: 'Host added to rudder' });
      }
    });
  } catch (err) {
    return res.status(500).json({ result: 'error', message: `${err}` });
  }

  return res.status(500).json({ result: 'error', message: 'Host not found' });
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在使用Object.keys(...).forEach(...)for 迭代第一个请求的结果。

供您参考,第一个请求的响应类似于:

{
    "action": "listPendingNodes",
    "result": "success",
    "data": {
        "nodes": [
            {
                "id": "f05f2bde-5416-189c-919c-954acc63dce7",
                "hostname": "foo",
                "status": "pending"
            },
            {
                "id": "b7f597ef-2b46-4283-bf70-d5e8cd84ba86",
                "hostname": "bar",
                "status": "pending"
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要迭代此输出并将每个输出的主机名字符串nodes与我的输入进行比较(来自脚本或邮递员的简单发布请求)。

如果两个字符串相等,则我的第二个请求将启动,并且必须在服务器中注册我的主机(此处的请求已简化,但您明白了)。

我的问题是我找不到使第二个请求起作用的方法。我尝试了不同的解决方案,并在一些论坛和网站上读到,同时使用循环和异步/等待很复杂,但是您对我有什么想法吗?

当我尝试我的代码时,我收到一条错误消息:

错误 [ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头

据我了解,forEach(...)不要等待我的第二次请求结果。所以return res.status(200).json({ result: 'success', message: 'Host added to rudder' });可能被称为另一种return res.status(500).json({ result: 'error', message: 'Host not found' });。我对吗 ?

我的意思是,真正的困难(或不是?)是Object.keys(...).forEach(...). 也许有一种简单的方法可以不使用此功能而使用另一个功能?或者重构我的代码?

问候。

Siv*_*ngh 7

异步等待在 forEach 循环中不起作用。将您的转换forEachfor循环。像这样:

let nodes = Object.keys(pendingNodes.data.data.nodes;
for (let i = 0; i < nodes.length; i++) {
    // Perform asynchronous actions and await them, it will work
    ...
}
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多内容:使用 async/await 和 forEach 循环

此外,函数是异步的,您可以在其中等待多个语句。你已经在前面编写了 async ,axios.get它基本上返回了一个承诺。你应该在等待那个。

将以下内容更改为

const response = async axios.get(`${config.rudderURL}/rudder/api/nodes/pending/${node.id}`, { headers });
Run Code Online (Sandbox Code Playgroud)

const response = await axios.get(`${config.rudderURL}/rudder/api/nodes/pending/${node.id}`, { headers });
Run Code Online (Sandbox Code Playgroud)