亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 324 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號