I'm trying to have the two lines in my initialize
method be run once so I can call other methods after this.browser
and this.page
are set. I can't do this in the constructor as they're async.
When I run this code I get UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'goto' of undefined
.
How can I set these variables so they can be used in other methods?
const puppeteer = require('puppeteer');
class Bot {
constructor() {}
async initialize() {
this.browser = await puppeteer.launch({ headless: false });
this.page = await this.browser.newPage();
}
async login() {
await this.page.goto('https://reddit.com/login/', { waitUntil: 'domcontentloaded' });
}
...
}
const bot = new Bot();
bot.initialize();
bot.login();