I'm trying to export an async function that returns object data to be used in another file... I'm having a lot of trouble. I would really appreciate any help.
Code:
const puppeteer = require('puppeteer');
(async () => {
let browser = await puppeteer.launch({ args: ['--no-sandbox'] , headless: true});
let page = await browser.newPage();
// JCC URL's:
let ph_jcc_pool_url = 'https://jcc.org/park-heights-indoor-pool-registration';
// go to park heights pool registration url:
await page.goto(ph_jcc_pool_url, {waitUntil: 'networkidle0'});
// evaluate park heights pool registration:
let scraped_ph_jcc_pool = await page.evaluate(() => {
let slots_array = [];
$(".GXPEntry").each(function (index, element) {
slots_array[index] = {
cancelled: null,
todaySlotTimePassed: null,
displayDayHeading: null,
signUpAble: null,
time: element.querySelector(".GXPTime").textContent,
dayOfWeek: $(element).parent().attr('id').replace("GXP", ""),
date: element.querySelector("a.descGXP").getAttribute("data-date"),
slotType: element.querySelector('.GXPTitle').textContent,
gender: null,
availability: null,
signUpLink: null,
appleCalendarLink: null,
location: element.querySelector(".GXPLocation").textContent,
studio: element.querySelector(".GXPStudio").textContent.slice(0, -1),
};
// Check if slot is cancelled or not
// - if cancelled, set as cancelled
// - if not, get data that only shows if item not cancelled
if (slots_array[index].slotType.includes("CANCELED") || slots_array[index].slotType.includes("CANCELLED")) {
slots_array[index].cancelled = true;
} else {
slots_array[index].cancelled = false;
slots_array[index].signUpLink = element.querySelector('a.signUpGXP').getAttribute("href");
slots_array[index].availability = element.querySelector('div.GXPDescription span').textContent;
}
// Check gender and set type string to .gender
if (slots_array[index].slotType.includes("Women")) {
slots_array[index].gender = "Female";
} else {
slots_array[index].gender = "Male";
}
}); // close .each(".GXPEntry") iterator function
return slots_array;
}); // close await page.evaluate jcc park heights
// park heights jcc pool registration data:
let ph_jcc_pool = {
main: scraped_ph_jcc_pool, // main entries to be displayed
taken_out: [], // to display in extra accordion style
days: [] // to display in nav
}
// add slotTimePassed data from time.js. test time for specific slots of TODAY
// --Returns--:
// FALSE: time has not passed and content should be displayed
// TRUE: time has passed and should be hidden
let time = require("./time");
ph_jcc_pool.main.forEach(function (object, index) {
// mark true or false if time has passed for each object
object.todaySlotTimePassed =
time.calculateIfSlotTimePassed(ph_jcc_pool.main[index].date, ph_jcc_pool.main[index].time);
// push objects that are (from today) in the past to new array
if (time.calculateIfSlotTimePassed(ph_jcc_pool.main[index].date, ph_jcc_pool.main[index].time)
=== true) {
ph_jcc_pool.taken_out.push(object);
}
}); // close time forEach()
// return false if no items have been taken out from today
if (ph_jcc_pool.taken_out.length === 0) {
ph_jcc_pool.taken_out = false;
}
// remove todaySlotTimePassed entries from ph_jcc_pool_data array
ph_jcc_pool.main = ph_jcc_pool.main.filter(function(object) {
return object.todaySlotTimePassed !== true;
})
// test when to put day heading. if true, then yes, if false, then no.
for (let i = 0; i<ph_jcc_pool.main.length; i++) {
if (i === 0) {
ph_jcc_pool.main[0].displayDayHeading = true;
ph_jcc_pool.days.push(ph_jcc_pool.main[0].dayOfWeek);
} else {
if (ph_jcc_pool.main[i - 1].dayOfWeek !== ph_jcc_pool.main[i].dayOfWeek) {
ph_jcc_pool.main[i].displayDayHeading = true;
ph_jcc_pool.days.push(ph_jcc_pool.main[i].dayOfWeek);
} else {
ph_jcc_pool.main[i].displayDayHeading = false;
}
}
}
await browser.close();
return ph_jcc_pool;
})(); // close async function
I'm really frustrated trying to figure this out. I need the variable/object ph_jcc_pool
to be accessible in the other file to be used with express, etc.
Thanks in advance if you can help!
This should work:
// in get-data.js
const puppeteer = require('puppeteer');
async function getData() {
const browser = await puppeteer.launch();
// ...
return data;
}
module.exports = {
getData,
};
// in main.js
const { getData } = require('./get-data.js');
(async function main() {
try {
const data = await getData();
console.log(data);
} catch (err) {
console.error(err);
}
})();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.