How do I call function outside async function in puppeteer with nodeJS

7

how do I call a function passing the page object outside async method in puppeteer.

I want to implement a functionality where if a certain value is found on page it should play a music. I have defined playmusic() functionality outside async method .

const puppeteer = require('puppeteer');

const playmusic = ()=>{
   page.goto('http://www.noiseaddicts.com/free-samples-mp3/?id=2544');
   page.click('span.map_play');
}

(async () => {
  const browser = await puppeteer.launch({headless : false});
  
  const page = await browser.newPage();

 
  await page.goto('DesiredURL');
  const pricing = await page.evaluate(()=>{
    let pricesOnPage=document.querySelectorAll(".span_price_wrap");
    const priceList=[...pricesOnPage];
    return priceList.map(h=>h.innerText);
    
  });
  console.log(pricing[1]);

  if(pricing[1]>=2316)
  {
    await page.evaluate(() => {
      playmusic ();
    });
  }

 await browser.close();
})();

I m getting below error

\index.js
2346.75
(node:17316) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: playmusic is not defined
    at __puppeteer_evaluation_script__:2:6
    at ExecutionContext._evaluateInternal (\jswebscrapping\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:217:19)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async ExecutionContext.evaluate (\jswebscrapping\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:106:16)
    at async index.js:24:4
(node:17316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17316) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

answered question

yes, that's how you call a function - are you having an issue with the code? Oh, I see now ...if you want to pass the page, you would playmusic(page) and define playmusic like const playmusic = page =>

yes I m getting an error while executing . Let me share the error . node .let me edit my question with error

nevermind, I see what you did wrong and suggested a fix

1 Answer

8

You need to replace

const playmusic = ()=>{
   page.goto('http://www.noiseaddicts.com/free-samples-mp3/?id=2544');
   page.click('span.map_play');
}

by

const playmusic = (page) => {
   page.goto('http://www.noiseaddicts.com/free-samples-mp3/?id=2544');
   page.click('span.map_play');
}

and

    await page.evaluate(() => {
      playmusic ();
    });

by

    await page.evaluate(() => {
      playmusic (page);
    });

Because of range when page exists is constrained and when you define function playmusic then this function do not have access to variable page. You can read more about variables scopes in JS.

Link to a great article:

https://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/

posted this

Have an answer?

JD

Please login first before posting an answer.