user3321541
user3321541

Reputation: 241

When I'm using phantomJS in node, the error appeared

I'm using phantomJS in node and wrote the following code, but the error appeared on the console.

The following code simply tries to access the google top page and take a screen shot of its page.

How should I resolve this error?

(node version: v6.10.3)

(1) code(test.js)

var phantom = require("phantom");
var promise = phantom.create().then(function(ph) {
    return ph.createPage().then(function(page) {
        page.property('onConsoleMessage', function(msg) {
            console.log(msg);
        });
        page.property('onLoadStarted', function() {
            loadInProgress = true;
            console.log('Load Started...');
        });
        page.property('onLoadFinished', function() {
            loadInProgress = false;
            console.log('Load End');
        });

        var loadInProgress = false;
        var stepIndex = 0;
        var steps = [
            function() {
                page.open('https://www.google.co.jp/').then(function (status) {
                    // something process
                }).catch(function(error){
                    console.log(error); // <======= Here, the error appeared
                });
            },
            function() {
                console.log("step end");
            }
        ];

        interval = setInterval(function(){
            if(!loadInProgress && typeof steps[stepIndex] == 'function') {
                steps[stepIndex]();
                stepIndex++;
            } else if (steps[stepIndex] != 'function') {
                ph.exit();
                clearInterval(interval);
            }
        }, 10);
    });
});

(2) error

$ node test.js
step end
warn: exit() was called before waiting for commands to finish. Make sure you are not calling exit() too soon.
info: Load Started...
Error: Phantom process stopped with exit code 0
    at Phantom._rejectAllCommands (<myfilepath>/phantomjs/node_modules/phantom/lib/phantom.js:361:41)
    at ChildProcess.Phantom.process.on.code (<myfilepath>/phantomjs/node_modules/phantom/lib/phantom.js:164:18)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)

Upvotes: 1

Views: 1214

Answers (1)

Vaviloff
Vaviloff

Reputation: 16838

You exit the script too early, exactly as the error says.

Remove ph.exit(); from the interval function:

    interval = setInterval(function(){
        if(!loadInProgress && typeof steps[stepIndex] == 'function') {
            steps[stepIndex]();
            stepIndex++;
        } else if (steps[stepIndex] != 'function') {
            // ph.exit(); // <-- remove exit() from here
            clearInterval(interval);
        }
    }, 10);

Move it instead into page.open callback for it to be called after the page is opened:

page.open('https://www.google.co.jp/').then(function (status) {
    ph.exit();
})

Upvotes: 1

Related Questions