我得到了一个
referenceError:终端A未定义
在第48线。我不知道我做错了什么。
writeStream.write(`Terminal A,Terminal B,Terminal C/D \n`);
var minutes = 1, timerInerval = minutes * 60 * 1000;
function TerminalOccupancyData() {
request('https://www.laguardiaairport.com/to-from-airport/parking', (error, response, html) => {
// Check there is no error
if (!error && response.statusCode == 200) {
// using cheerio library to load the website page html
const $ = cheerio.load(html);
$('.terminal-left').each((span, el) => {
// Find the element using the class
const terminalA = $(el)
.find('.terminal-percentage')
.text()
.replace(/% Full/, '');
const terminalB = $(el)
.find('.terminal-percentage')
.text()
.replace(/% Full/, '');
const terminalCD = $(el)
.find('.terminal-percentage')
.text()
.replace(/% Full/, '');
});
console.log('\nTerminal Data scraped ... \n');
}
});
// Export to file to upload to database
writeStream.write(`${terminalA},${terminalB},${terminalCD} \n`);
}
setInterval(TerminalOccupancyData, timerInerval);发布于 2021-11-17 23:34:28
这里存在的问题是,您的writeStream.write(...)与它所引用的变量不在同一范围内。由于您不试图从外部函数返回任何值,我相信解决问题的方法就是将该语句移到.each()回调函数中,如下所示:
function TerminalOccupancyData() {
request('https://www.laguardiaairport.com/to-from-airport/parking', (error, response, html) => {
// Check there is no error
if (!error && response.statusCode == 200) {
// using cheerio library to load the website page html
const $ = cheerio.load(html);
$('.terminal-left').each((span, el) => {
// Find the element using the class
const terminalA = $(el)
.find('.terminal-percentage')
.text()
.replace(/% Full/, '');
const terminalB = $(el)
.find('.terminal-percentage')
.text()
.replace(/% Full/, '');
const terminalCD = $(el)
.find('.terminal-percentage')
.text()
.replace(/% Full/, '');
// Export to file to upload to database
writeStream.write(`${terminalA},${terminalB},${terminalCD} \n`);
});
console.log('\nTerminal Data scraped ... \n');
}
});
}
setInterval(TerminalOccupancyData, timerInerval);https://stackoverflow.com/questions/70012726
复制相似问题