Skip to content
Advertisement

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

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement