AmazonPricesMonitoring icon indicating copy to clipboard operation
AmazonPricesMonitoring copied to clipboard

Getting (node:32312) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation intermittently.

Open JyotiPrakashMallick opened this issue 4 years ago • 0 comments

Tom,

Thanks for sharing the code repo, I was trying the same for UDEMY. And I am getting (node:32312) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation intermittently.

Here is my sample code:

const puppeteer = require('puppeteer');
const $ = require('cheerio');
const CronJob = require('cron').CronJob;
const nodemailer = require('nodemailer');

const url = 'https://www.udemy.com/course/az-300-azure-architecture-technologies-practice-test/';

async function configureBrowser() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);
	await page.waitForNavigation({ waitUntil: 'networkidle0' });
    return page;
}

async function checkPrice(page) {
    await page.reload();
    let html = await page.evaluate(() => document.body.innerHTML);
    // console.log(html);
    await page.waitForNavigation({ waitUntil: 'networkidle0' });
    const coursePrice = await page.$eval('#udemy > div.main-content-wrapper > div.main-content > div.paid-course-landing-page__container > div.top-container.dark-background > div > div > div.course-landing-page__main-content.course-landing-page__purchase-section__main > div > div.main-cta-container > div.buy-box-main > div > div > div:nth-child(2) > div > div > div > div > div > div.price-text--price-part--Tu6MH.udlite-clp-discount-price.udlite-heading-xl > span:nth-child(2) > span', el => el.innerText);
    let currentPrice = Number(coursePrice.replace(/[^0-9.-]+/g, ""));
    console.log("---->"+currentPrice)
	if (currentPrice < 500) {
        console.log("BUY!!!! " + currentPrice);
        sendNotification(currentPrice);
    }
    else {
        console.log("Not a good time to buy, current price hold at ---->" + currentPrice);
    }
}

// async function startTracking() {
//     const page = await configureBrowser();
//   // let job = new CronJob('* */30 * * * *', function() { //runs every 30 minutes in this config
//     let job = new CronJob('*/3 * * * * *', function() { //runs every 30 minutes in this config
//       checkPrice(page);
//     }, null, true, null, null, true);
//     job.start();
// }

async function sendNotification(price) {

    let transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: '[email protected]',
        pass: '<app paasword>'
      }
    });
  
    let textToSend = 'Price dropped to ' + price;
    let htmlText = `<a href=\"${url}\">Link</a>`;
  
    let info = await transporter.sendMail({
      from: '"Price Tracker" <*****@gmail.com>',
      to: "[email protected]",
      subject: 'Price dropped to ' + price, 
      text: textToSend,
      html: htmlText
    });
  
    console.log("Message sent: %s", info.messageId);
  }

//startTracking();

async function monitor() {
    let page = await configureBrowser();
    await checkPrice(page);

}


monitor();

While running I am getting intermittently, like first run it triggered successfully and got node udemyTracker Not a good time to buy!!!! 455

and in the second try I am getting error

(node:32256) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
    at rewriteError (C:\Users\MallicJ\Downloads\Support\MYVISIT\FrameworkP\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:261:23)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async ExecutionContext._evaluateInternal (C:\Users\MallicJ\Downloads\Support\MYVISIT\FrameworkP\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:215:61)
    at async ExecutionContext.evaluate (C:\Users\MallicJ\Downloads\Support\MYVISIT\FrameworkP\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:106:16)
    at async checkPrice (C:\Users\MallicJ\Downloads\Support\MYVISIT\FrameworkP\udemyTracker.js:19:16)
    at async monitor (C:\Users\MallicJ\Downloads\Support\MYVISIT\FrameworkP\udemyTracker.js:38:9)
(node:32256) 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:32256) [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.

JyotiPrakashMallick avatar Oct 15 '20 04:10 JyotiPrakashMallick