慕婉清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;
};
添加回答
舉報
0/150
提交
取消