0

I am trying to access a variable that i declared inside in an async function. Like this:

exports.startTheBasketEngine = async (username, password, domainData) => {
    const parse = domainData.auctionUrl.split("-")
    var domainName = parse[0]

    const stringIsIncluded = await page.evaluate(() => {
        console.log(domainName)
     });
}

The console.log line is returning that error:

 UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: domainName is not defined

How can i resolve this problem?

2
  • Works for me locally. We need to see more code to evaluate the problem. What's in page.evaluate() ? Commented Jul 8, 2020 at 12:10
  • Have you tried something like this: await page.evaluate((dName) => { console.log(dName); }, domainName);? Commented Jul 8, 2020 at 12:14

3 Answers 3

1

I assume you use Puppeteer.

According to the docs, the .evaluate() takes more arguments. All arguments, starting from the second, will be passed to the pageFunction. So try something like this:

var domainName = parse[0]

const stringIsIncluded = await page.evaluate((dName) => {
    console.log(dName);
 }, domainName);
Sign up to request clarification or add additional context in comments.

Comments

1

You should pass the value to evaluate, it is executed in a separate context, see: https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pageevaluatepagefunction-args

const stringIsIncluded = await page.evaluate((domainName) => {
    console.log(domainName)
}, domainName);

Comments

0

You have to pass your variable:

 const stringIsIncluded = await page.evaluate((domainName) => {
    console.log(domainName)
 }, domainName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.