首页 文章

为什么在包装在exports.handler中时,Lambda上的代码出错?

提问于
浏览
0

我正在使用这个casperjs nodejs app:https://github.com/narainsagar/node-casperjs-aws-lambda

我已经让我的代码在本地工作,并在上传到Lambda时,但是我需要将我的代码包装在exports.handler中,这样我就可以从API网关向函数传递数据 . (我已经使用其他未使用此应用程序的函数执行此操作 . )当我将我的代码放在exports.handler中时,它会失败 . 我已经测试过将事件数据添加到lambda中的测试事件中,我尝试过对其进行硬编码 . 当我的代码在exports.handler中时,两者都失败了 . 为什么exports.handler会破坏这段代码?

没有exports.handler的工作代码:

var ua = 'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
var casper = require('casper').create({
    viewportSize: {width: 768, height: 1024},
    userAgent: ua
});
var login = {email: 'fakeemail@gmail.com', pw: 'fakepw'}
var usrObj = {
    succ: {},
    err: []
};

//exit function
function exit() {
    setTimeout(function() {
        casper.exit();
        casper.bypass(1);
    }, 10);
}
//success & error message function
function message(dat) {
    if (dat === 'credentials') {
        console.log('Error: Login credentials are missing');
        return exit();
    }
    else if (dat) {
        console.log(dat);
        return exit();
    }

    if (usrObj['err'].length > 0) {
        console.log('Error not empty...');
        console.log(usrObj.err);
        return exit();
    }
    else if (usrObj['succ']) {
        console.log('Success not empty...');
        console.log(JSON.stringify(usrObj.succ));
        return exit();
    }

}

//trim login credentials
login.email = login.email.trim();
login.pw = login.pw.trim();

if (login.email && login.pw) {

    casper.start('https://vimeo.com/log_in');
    casper.waitForSelector('form#login_form',
        function success() {
            this.echo(this.getCurrentUrl());
            this.sendKeys('form#login_form input[name="email"]', login.email);
            this.sendKeys('form#login_form input[name="password"]', login.pw);
            this.click('form#login_form input[type=submit][value="Log in with email"]');
        },
        function fail() {
            message('Error with Vimeo [page not loading]')
        }
    );
    casper.waitForSelector('#page_header>h1>a',
        function success() {
            this.echo(this.getCurrentUrl()); 
            usrObj['succ']['uname'] = this.getHTML('span.topnav_user_name');
            usrObj['succ']['profile'] = this.getElementAttribute('li.topnav_user_profile>a', 'href');
            var test = [];
            if (!usrObj.succ.uname) {test.push('Username not retrieved')}
            if (!usrObj.succ.profile) {test.push('Profile link not retrieved')}
            if (test.length > 0) {message(test.join('
'));} //else {message();} }, function fail() { message('Login not successful'); } ); casper.thenOpen('https://vimeo.com/staceydavidgearz', function success() { this.echo('Stacey David Profile: ' + this.getTitle()); this.echo(this.getCurrentUrl()); var finish = function() { usrObj['succ']['foll'] = true; message(); } //var foll = this.getHTML('button[data-fatal-attraction="container:profile_page|component:follow"] > span'); var foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox'); if (foll === '0 0 10 10') { this.click('button[data-fatal-attraction="container:profile_page|component:follow"]'); setTimeout(function() { foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox'); if (foll === '0 0 10 10') { message('Can\'t follow SD'); } else {finish();} }, 250); } else { finish(); } }, function fail() { message('Not going to Stacey David profile page.'); } ); casper.run(); } else {message('credentials');}

exports.handler中的代码失败:

var ua = 'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';

var casper = require('casper').create({
    viewportSize: {width: 768, height: 1024},
    userAgent: ua//,
    //verbose: true,
    //logLevel: 'debug'
});

exports.handler = function (event, context) {

    var login = {};
    var usrObj = {
        succ: {},
        err: []
    };
    //exit function
    var exit = function() {
        setTimeout(function() {
            casper.page.close();
            casper.exit();
            casper.bypass(1);
        }, 10);
    };
    //success & error message function
    var message = function(dat) {
        if (dat === 'credentials') {
            usrObj['err'].push('Error: Login credentials are missing');
        }
        else if (dat) {
            usrObj['err'].push(dat);
        }

        if (usrObj['err'].length > 0) {
            console.log('Error not empty...');
            console.log(JSON.stringify(usrObj.err));
            context.fail(JSON.stringify(usrObj.err));
            return exit();
        }
        else if (usrObj['succ']) {
            console.log('Success not empty...');
            console.log(JSON.stringify(usrObj.succ));
            context.succeed(JSON.stringify(usrObj.succ));
            return exit();
        }
    };

    //trim login credentials
    login.email = event.email;
    login.pw = event.pw;

    if (login.email && login.pw) {

        casper.start('https://vimeo.com/log_in');
        casper.waitForSelector('form#login_form',
            function success() {
                this.echo(this.getCurrentUrl());
                this.sendKeys('form#login_form input[name="email"]', login.email);
                this.sendKeys('form#login_form input[name="password"]', login.pw);
                this.click('form#login_form input[type=submit][value="Log in with email"]');
            },
            function fail() {
                message('Error with Vimeo [page not loading]')
            }
        );
        casper.waitForSelector('#page_header>h1>a',
            function success() {
                this.echo(this.getCurrentUrl()); 
                usrObj['succ']['uname'] = this.getHTML('span.topnav_user_name');
                usrObj['succ']['profile'] = this.getElementAttribute('li.topnav_user_profile>a', 'href');
                var test = [];
                if (!usrObj.succ.uname) {test.push('Username not retrieved')}
                if (!usrObj.succ.profile) {test.push('Profile link not retrieved')}
                if (test.length > 0) {message(test.join('
'));} //else {message();} }, function fail() { message('Login not successful'); } ); casper.thenOpen('https://vimeo.com/staceydavidgearz', function success() { this.echo('Stacey David Profile: ' + this.getTitle()); this.echo(this.getCurrentUrl()); var finish = function() { usrObj['succ']['foll'] = true; message(); } var foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox'); if (foll === '0 0 10 10') { this.click('button[data-fatal-attraction="container:profile_page|component:follow"]'); setTimeout(function() { foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox'); if (foll === '0 0 10 10') { message('Can\'t follow SD'); } else {finish();} }, 250); } else { finish(); } }, function fail() { message('Not going to Stacey David profile page.'); } ); casper.run(); } else {message('credentials');} };

Lambda回复:

身体

[]

日志:

Calling casperJS:  /var/task/node_modules/casperjs/bin/casperjs [ '/var/task/casperjs-script.js' ] { PHANTOMJS_EXECUTABLE: '/var/task/phantomjs' }
child process exited with code 1

2 回答

  • 0

    确保还使用casper组件上载依赖项“node_modules” . 给出错误是因为在lambda执行中没有在require中找到该组件 .

  • 0

    请点击此链接:https://github.com/narainsagar/node-casperjs-aws-lambda/issues/6#issuecomment-279177650

    嗨,您可以通过casper args支持将数据传递给scraper脚本:在index.js文件中对runCasper函数进行一些更改,如下所示:var childArgs = [
    path.join(__ dirname,scriptName),
    ***@***.***”,
    '--password = 12345'
    ] .
    在你的casper-script中,你可以获得如下所示的参数数据:var utils = require('utils');

    utils.dump(casper.cli.get( '电子邮件'));
    utils.dump(casper.cli.get( '密码'));
    // 要么
    // utils.dump(casper.cli.raw.get('email'));
    // utils.dump(casper.cli.raw.get('password'));
    希望这可以帮助..

    干杯,

    纳拉因

相关问题