Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

294
Views
Passing Variable to page.evaluate() function

I'm trying to pass a variable I am using in my code into page.evaluate function, but it's console.logging as it's not defined and creating errors for my code.

const putterID = puttersObjectArray[putterPriorityIndexFound].id;
console.log(putterID) // 7729

    await page.evaluate(async () => {
        console.log(putterID)
        await fetch(`https://www.examplestore.com/store/product/addtocartplp/?productId=${putterID}&X-Requested-With=XMLHttpRequest&_=1647970037149`, {
            "headers": {
            "accept": "*/*",
            "accept-language": "en-US,en;q=0.9",
            "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-newrelic-id": "VQABWFZUCBAEXFRRAQkCVVc=",
            "x-requested-with": "XMLHttpRequest"
            },
            "referrer": "https://www.examplestore.com/store/products/",
            "referrerPolicy": "no-referrer-when-downgrade",
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "include"
        })
        .then(response => response.json())
        .then(data => console.log(data))
        .catch(error => console.log(error));
    });

I get the following error: (node:3210) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: putterID is not defined

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Can you please try with this. Here, i have first check for putterId then it will execute async code.

const putterID = puttersObjectArray[putterPriorityIndexFound].id;
putterID && page.evaluate(async () => {
        console.log(putterID)
        await fetch(`https://www.examplestore.com/store/product/addtocartplp/?productId=${putterID}&X-Requested-With=XMLHttpRequest&_=1647970037149`, {
            "headers": {
            "accept": "*/*",
            "accept-language": "en-US,en;q=0.9",
            "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-newrelic-id": "VQABWFZUCBAEXFRRAQkCVVc=",
            "x-requested-with": "XMLHttpRequest"
            },
            "referrer": "https://www.examplestore.com/store/products/",
            "referrerPolicy": "no-referrer-when-downgrade",
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "include"
        })
        .then(response => response.json())
        .then(data => console.log(data))
        .catch(error => console.log(error));
    });

about 4 years ago · Juan Pablo Isaza Report

0

Update: I need to pass putterID in like this:

const putterID = puttersObjectArray[putterPriorityIndexFound].id;
console.log(putterID) // 7729

    await page.evaluate(async () => {
        console.log(putterID)
        await fetch(`https://www.examplestore.com/store/product/addtocartplp/?productId=${putterID}&X-Requested-With=XMLHttpRequest&_=1647970037149`, {
            "headers": {
            "accept": "*/*",
            "accept-language": "en-US,en;q=0.9",
            "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-newrelic-id": "VQABWFZUCBAEXFRRAQkCVVc=",
            "x-requested-with": "XMLHttpRequest"
            },
            "referrer": "https://www.examplestore.com/store/products/",
            "referrerPolicy": "no-referrer-when-downgrade",
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "include"
        })
        .then(response => response.json())
        .then(data => console.log(data))
        .catch(error => console.log(error));
    }, putterID);
about 4 years ago · Juan Pablo Isaza Report

0

const putterID = puttersObjectArray[putterPriorityIndexFound].id; console.log(putterID) // 7729

await page.evaluate(async (putterID) => {
    console.log(putterID)
    await fetch(`https://www.examplestore.com/store/product/addtocartplp/?productId=${putterID}&X-Requested-With=XMLHttpRequest&_=1647970037149`, {
        "headers": {
        "accept": "*/*",
        "accept-language": "en-US,en;q=0.9",
        "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"macOS\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "x-newrelic-id": "VQABWFZUCBAEXFRRAQkCVVc=",
        "x-requested-with": "XMLHttpRequest"
        },
        "referrer": "https://www.examplestore.com/store/products/",
        "referrerPolicy": "no-referrer-when-downgrade",
        "body": null,
        "method": "GET",
        "mode": "cors",
        "credentials": "include"
    })
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.log(error));
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!