I'm Trying to take a screenshot from a website using the package puppeteer
For this I create a simple server with Express:
app.get('/requestScreenShootForDesktop/:id', function(req, res){
(async () => {
const pathUpload = 'uploads/' + Math.floor(Date.now() / 1000) + '.png';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(this.req.params.id);
await page.setViewport({width: 1920, height: 1080});
await page.screenshot({path: pathUpload});
await browser.close();
await res.send({msg: 'ScreenShot Ok'});
})();
});
The problem with this code is on line await page.goto(this.req.params.id);, Node says:
Cannot read property 'params' of undefined
This is because the variable req belonging to function app.get doesn't exists in async scope.
How can I solve this problem, and pass my variable to async function ?
reqdoesn't exist, it's thatthis.reqdoesn't exist. Why did you usethis? Overall it's not really clear what you intend for that code to do.this.reqshould bereq.