简体   繁体   English

在Node.js / Express.js上重构Phantom.js以避免“回调地狱”时,Phantom.js回调参考错误

[英]Phantom.js callback Reference errors when refactoring Phantom.js on Node.js/Express.js to avoid “callback hell”

I have this crazy large block of code that functions, but which I am hoping to refactor properly. 我有这个疯狂的大功能代码块,但是我希望可以正确地重构它。 With accordance to Callback Hell , I tried to break it down into non-anonymous functions and seperate those functions from the central code. 根据Callback Hell ,我试图将其分解为非匿名函数,并将这些函数与中央代码分开。

However, I am running into the problem that a lot of the different sections of the code are dependent on using the others as parameters. 但是,我遇到了一个问题,代码的许多不同部分都依赖于将其他部分用作参数。 The first error message I received in the sequence is ReferenceError: page is not defined 我在序列中收到的第一条错误消息是ReferenceError: page is not defined

The un-refactored code is: 未重构的代码是:

function startMyFunction(firstLayerUrl) {
        phantom.create(function (ph) {
            ph.createPage(function (page) {
                var main_file=firstLayerUrl
                page.open(main_file, function (status) {
                    var linkArray=[];
                    page.evaluate(function (linkArray) {
                        for (var i=0; i < document.getElementsByTagName('a').length; i++) {
                            linkArray.push(document.getElementsByTagName('a')[i].href)
                        }
                        return linkArray;
                    }
                    , function finished(result) {
                        linkArray = result;
                        runEmailLoop(linkArray);
                        page.close()
                        ph.exit();
                    },linkArray);
                });
            });
        }, {
            dnodeOpts: {
                weak: false
            }
        });
}

The attempted refactored code is: 尝试的重构代码为:

function runFirstLayer(firstLayerUrl) {
    phantom.create(function (ph) {
        ph.createPage(function (page) {
            var main_file=firstLayerUrl
            page.open(main_file, openIndexPage(status));
        });
    }, {
        dnodeOpts: {
            weak: false
        }
    });
}

function openIndexPage (status) {
    var linkArray=[];
    page.evaluate(evaluatePage(linkArray), finished(result),linkArray);
}

function evaluatePage(linkArray) {
    for (var i=0; i < document.getElementsByTagName('a').length; i++) {
        linkArray.push(document.getElementsByTagName('a')[i].href)
    }
    return linkArray;
}

function finished(result) {
    linkArray = result;
    runEmailLoop(linkArray);
    page.close()
    ph.exit();
}

How can I pass the various dependencies on, such as page , status , since I have tried passing page but then get another error ReferenceError: document is not defined 如何传递各种依赖项,例如pagestatus ,因为我尝试传递页面,但是又遇到另一个错误ReferenceError: document is not defined

You need to pass the functions, not call them: 您需要传递函数,而不是调用它们:

page.open(main_file, openIndexPage);
/* instead of
page.open(main_file, openIndexPage(status));
*/

and

page.evaluate(evaluatePage, finished, linkArray);
/* instead of
page.evaluate(evaluatePage(linkArray), finished(result),linkArray);
*/

There are no status , linkArray , or result values in your code - they are parameters of the function definitions. 您的代码中没有statuslinkArrayresult值-它们是函数定义的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM