API调用期间的NodeJs MaxListenersExceededWarning

我正在用风数据(从 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


  });



慕婉清6462132
浏览 303回答 1
1回答

海绵宝宝撒

您每秒都在启动一个新的浏览器实例,并且不要关闭它们。您的代码执行此操作:  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;};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript