question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't pass function to evaluationContext (Improvement?)

See original GitHub issue

Steps to reproduce

Tell us about your environment:

What steps will reproduce the problem?

Please include code that reproduces the issue.

see https://github.com/Vandivier/data-science-practice/tree/function-to-evaluation-context-repro

clone and execute npm run-script get-riders

What is the expected result?

Objects will be printed to console which contain the key-value pair ‘hi’, ‘sup’, as well as a key ‘fpEvaluateInPage’ with a function as the value. Additionally, this function should be executed, printing ‘hi’ to the console on it’s own line.

What happens instead? Objects are printed to console only containing the key-value pair ‘hi’, ‘sup’

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

18reactions
aslushnikovcommented, Jan 12, 2018

@Vandivier if I understand correctly, you’re passing function from node.js context over to the page context as one of the arguments in the page.evaluate:

const puppeteer = require('puppeteer');

(async() => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  const myObject = {fn: e => 7 + e};
  const result = await page.evaluate(myObject => myObject.fn.call(1), myObject);
  console.log(result);

  await browser.close();
})();

This won’t work since myObject, and specifically function inside it, is not Serializable. To pass function over to the page context, you’ll have to stringify it first and then re-create in browser:

const puppeteer = require('puppeteer');

(async() => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  const myFunction = e => e + 1;
  const result = await page.evaluate(myFunctionText => {
    // re-creating function in browser context
    const myFunction = new Function(' return (' + myFunctionText + ').apply(null, arguments)');
    return myFunction.call(null, 7);
  }, myFunction.toString());
  console.log(result);

  await browser.close();
})();
9reactions
Vandiviercommented, Jan 12, 2018

lol. Since when is “I can’t understand” a reason to close an issue?

Here’s the issue in it’s simplest form. Consider the following object:

let options = {
    'fpEvaluateInPage': function (_options) {
        console.log('hi');
        return Promise.resolve({
            'sTableHtml': 'hi'
        });
    },
    'hi': 'sup'
}

If this object is passed as options to evaluationContext, options.hi exists and options.fpEvaluateInPage does not exist. Or at very least the console.log('hi') fails to print in combination with _page.on('console', _fCleanLog);, where _fCleanLog is defined as follows:

function _fCleanLog(ConsoleMessage) {
    console.log(ConsoleMessage.text + EOL);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I pass variable into an evaluate function?
For pass a function , there are two ways you can do it. // 1. Defined in evaluationContext await page.evaluate(() => { window....
Read more >
StartResourceEvaluation - AWS Config
Runs an on-demand evaluation for the specified resource to determine whether the resource details will comply with configured AWS Config rules.
Read more >
Advanced features — Cottle Documentation 2.0.0 ...
The callback you'll pass to Function takes multiple arguments: First argument is always an internal state that must be forwarded to any nested...
Read more >
How to execute Context, Input, Process, and Product ... - NCBI
Improvements to education are necessary in order to keep up with the education ... the evaluation method itself cannot assume the role of...
Read more >
7 Environments | Advanced R - Hadley Wickham
How do you determine the environment from which a function was called? How are <- and <<- different? ... We can't go any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found