PRVS
PRVS

Reputation: 1690

PhantomJS - page.evaluate is not a function

var phantom = require('phantom');

phantom.create()
        .then(function (ph) {
            _ph = ph;
            return ph.createPage();
        })
        .then(function(page) {
            _page = page;
            url = "http://www.aeiou.pt";
            return page.open(url);
        })
        .then(function(page) {

        console.log("hello3");
            page.evaluate(function () {

My code starts with something like this. The console.log "hello3" is printed but then, it gives me error:

TypeError: page.evaluate is not a function at /home/someone/server123.js:58:11 at at process._tickCallback (internal/process/next_tick.js:188:7)

Why it happens in this situation?

Node version: v8.6.0

Npm version: 5.3.0

Phantom version: [email protected]

Upvotes: 0

Views: 1395

Answers (1)

Mark
Mark

Reputation: 92440

The problem you are having is that page.open() does not return the page -- it returns the status. So the value being passed to the next then() is the status, which you try to call evaluate on that. This, of course, doesn't work.

The way they handle this in their example is have a page variable outside the then() chain that they can access inside each then(). You are almost doing that with _page = page; If _page is defined outside the function, you should be able to call _page.evaluate() rather than calling it on the return value from open().

var phantom = require('phantom');
var _page;

phantom.create()
    .then(function (ph) {
        _ph = ph;
        return ph.createPage();
    })
    .then(function(page) {
        _page = page;
        url = "http://www.aeiou.pt";
        return page.open(url);
    })
    .then(function(status) {
        // check status for errors here
        console.log("hello3");
        _page.evaluate(function () {

Upvotes: 2

Related Questions