简体   繁体   中英

casper js how can i pass varibales from one homade function to another

okay so i have started building my own modules to use in casperjs i have come to a point where i want to pass variable from one function to another here is what i have below

my modules -- functions

exports.accdata = function(accnum, amnum) {
    var accountnumber = casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + accnum + ') > td:nth-child(2) > a:nth-child(1)');
    var amountwithtype =casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + amnum + ') > td:nth-child(4) > div:nth-child(1)');
    var redir = accountnumber.substr(1);
    var split = amountwithtype.split (' '); 
    var amount = split[0];
    var type = split[1];
};

exports.job = function (in1, in2){
    console.log(in1);
    console.log(in2);
};

and then how i am trying to pass is my casperjs script

casper.then(function(){
   universe.accdata("3", "3");
   universe.job(amount, type);
});

i am very new and really need help on how to set functions to pass data from on and another

Your accdata function is just setting a bunch of variables then not using them. The simplest solution is to put both functions into one but another solution might be to return an object from your first function and pass it as a parameter to the second function.

exports.accdata = function(accnum, amnum) {
    return {
        accountnumber: casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + accnum + ') > td:nth-child(2) > a:nth-child(1)'),
        amountWithType: casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + amnum + ') > td:nth-child(4) > div:nth-child(1)'),
        ...
    };
};

exports.job = function(data) {
    console.log(data);
};

Then use it like this:

casper.then(function() {
    universe.job(universe.accdata("3", "3"));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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