首页 > 解决方案 > API调用期间的NodeJs MaxListenersExceededWarning

问题描述

我正在用风数据(从 API JSON 中抓取)填充一个数组,但出现MaxListenersExceededWarning错误。我查了一下,似乎是由于代码中的错误。一种解决方法是设置setMaxListeners(n);,但显然不建议这样做。

谁能看到是什么导致这么多听众注册?有什么解决办法?我正在创建一个windRecordings在请求时吐出数组的 API。

代码

const getWindForecast = (windRecordings) => {
  setInterval(() => {
    const instantWind = scrapeAPI(
      "http://mobvaer.kystverket.no/v2/api/stations/5265049"
    );
    instantWind.then((res) => {
      if (windRecordings.length > 0) {
        // A wind value(s) is already pushed to the list
        const latestRecordedWind = windRecordings[windRecordings.length - 1]; // get the first element out
        
        // Compare the lates wind value in the list to the lates API request wind value
        if (
          latestRecordedWind[1]["Value"]["Value"] == res[1]["Value"]["Value"]
        ) {
          console.log("They are the same");
        } else {
            console.log("They are not the same, push them.")
          windRecordings.push(res);
        }
      } else {
        // The length is less than 0, no element has been added so far, push element
        console.log("Push the first to the list");
        windRecordings.push(res);
      }
    });

    return windRecordings;
  }, 1000);
};

错误

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:85830) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:85830) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGTERM listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:85830) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGHUP listeners added to [process]. Use emitter.setMaxListeners() to increase limit

scrapeAPI 函数

const puppeteer = require("puppeteer");

const scrapeAPI = async (url) => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);
  var content = await page.content();
  innerText = await page.evaluate(() => {
    return JSON.parse(document.querySelector("body").innerText);
  });

  const instantWind = innerText["Instantaneous"];
  return instantWind;
};

干杯!

标签: javascriptnode.jsarrayspuppeteer

解决方案


您每秒钟都在启动一个新的浏览器实例,并且不要关闭它们。

您的代码执行此操作:

  setInterval(() => {
    //const instantWind = scrapeAPI();
...
  const browser = await puppeteer.launch();
...
  }, 1000);

您需要关闭重用浏览器实例或至少关闭它们:

const scrapeAPI = async (url) => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);
  var content = await page.content();
  innerText = await page.evaluate(() => {
    return JSON.parse(document.querySelector("body").innerText);
  });

  const instantWind = innerText["Instantaneous"];
await browser.close();
  return instantWind;
};


推荐阅读