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
CodePudding user response:
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));
});
CodePudding user response:
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);
CodePudding user response:
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));
});