为了账号安全,请及时绑定邮箱和手机立即绑定

API调用期间的NodeJs MaxListenersExceededWarning

API调用期间的NodeJs MaxListenersExceededWarning

慕婉清6462132 2023-03-24 14:23:31
我正在用风数据(从 API JSON 中抓取)填充一个数组,但出现错误MaxListenersExceededWarning。我已经查过了,这似乎是由于代码中的错误造成的。解决方法是设置setMaxListeners(n);,但显然不推荐这样做。任何人都可以看到是什么导致注册了这么多听众吗?什么是解决方案?我正在创建一个在请求时吐出数组的 API windRecordings。代码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  });
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

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


您的代码执行此操作:


  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;

};


查看完整回答
反对 回复 2023-03-24
  • 1 回答
  • 0 关注
  • 279 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信