Error: Evaluation failed: ReferenceError: res is not defined

12,858

Official document - The method returns a Promise which resolves to the return value of pageFunction.

This mean you can get back a value in "outside" of evaluate function.

//...
const result = await page.evaluate(() => {
    var hotelsElms = document.querySelectorAll('.keyword._ngcontent-vyt-82');
    var hotelJson = {};
    hotelsElms.forEach((hotelelement) => {
        hotelJson[name] = hotelelement.querySelector('span.keyword._ngcontent-vyt-82').innerText;
    });
    return hotelJson; // return value to "out site", assign `hotelJson` to `result`
});

// response the data to the client
res.json(result);

// ...
Share:
12,858
Admin
Author by

Admin

Updated on June 27, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a scope problem in node with puppeteer. I cannot send what is obtained in the evaluate object of puppeteer, because I get: Error: Evaluation failed: ReferenceError: res is not defined I need help.

    const express = require('express');
    const path = require('path'); // NEW
    const puppeteer = require('puppeteer');
    const fs = require('fs');
    
    const config = require('../src/config.js');
    const app = express();
    const port = process.env.PORT || 3006;
    const DIST_DIR = path.join(__dirname, '../dist'); // NEW
    const HTML_FILE = path.join(DIST_DIR, 'index.html'); // NEW
    
    app.use(express.static(DIST_DIR)); // NEW
    app.get('/api', (req, res) => {
      res.send(mockResponse);
    });
    app.get('/getapi', async (req, res) => {
      req.setTimeout(500000);
    
      var pal= req.query.palabra;
      function randomIntFromInterval(min, max) { // min and max included
        return Math.floor(Math.random() * (max - min + 1) + min);
      }
    
      const next_class = 'snByac';
    
          const browser = await puppeteer.launch({
          headless: false,
        });
          const page = await browser.newPage();
    
          await page.goto('https://adwords.google.com/ko/KeywordPlanner/Home?', {waitUntil: 'networkidle2'});
          await page.waitFor(randomIntFromInterval(10000,25000));
          await page.type('input[name=identifier]', config.mail);
          await page.click('span.' + next_class);
          await page.waitFor(randomIntFromInterval(20000,23000));
          await page.type('input[name=password]', config.password)
          await page.click('span.' + next_class)
          await page.waitFor(randomIntFromInterval(37000,45000));
          await page.click('[icon="arrow_forward"]')
          await page.waitFor(randomIntFromInterval(3800,6000));
          await page.type('[aria-autocomplete="list"]', pal)
          await page.waitFor(randomIntFromInterval(1400,2400));
          await page.keyboard.press('Enter');
          await page.waitFor(randomIntFromInterval(5000,14000));
          await page.keyboard.press('Enter');
          var hotelJson = {};
          await page.evaluate(() => {
          var hotelsElms = document.querySelectorAll('.keyword._ngcontent-vyt-82');
          hotelsElms.forEach((hotelelement) => {
           hotelJson.name = hotelelement.querySelector('span.keyword._ngcontent-vyt-82').innerText;
            });
          res.json(hotelJson);
    });
    
    
            await browser.close();
    
    });
    app.get('/', (req, res) => {
     res.sendFile(HTML_FILE); // EDIT
    });
    app.listen(port, function () {
     console.log('App listening on port: ' + port);
    });
    //app.setTimeout(500000);
    

    I have also tried putting "res.json (hotelJson);" outside of the "evaluate" object but returns an empty json, it is as if "hotelJson" could not be assigned within the scope of the "evaluate" function and then reverted empty, I tried to fill it with false data and it also returns empty.

  • MechaCode
    MechaCode almost 4 years
    Modifiying compilerOptions in tsconfig.json solved the issue - github.com/puppeteer/puppeteer/issues/…