简体   繁体   中英

Pass additional parameters to Javascript request 3 argument callback

I am trying to nest several callbacks with the goal of programmatically navigating a page using POST requests. I have a list of different files I am trying to download, and want to assign each of them a different filename so that I can make asynchronous download calls and perform OCR on them.

The callback that actually downloads the PDF is here:

filename = "doodle" + Math.floor((Math.random() * 100) + 1) + ".pdf";    
request.post(
                {
                    url:'https://ecorp.sos.ga.gov/BusinessSearch/DownloadFile',
                    form: {
                        'documentId': body
                    },
                    headers: {
                        'Referer': 'https://ecorp.sos.ga.gov/BusinessSearch/BusinessFilings'
                    }
                }, getPDF).pipe(fs.createWriteStream(filename));

Notice that the request sends the resulting page to a getPdf function, which actually opens the PDF for OCR after the request has been made. Currently I have a filename hardcoded in the OCR method, because I do not know how to pass an additional variable to the callback function. I would like to be able to write to a randomly generated file, and in the callback method retrieve that filename.

function getPDF (error, response, body) {
    if(!error){
        console.log(body);


        filename = "/doodle" + Math.floor((Math.random() * 100) + 1) + ".pdf";
        //console.log(__dirname + filename);

        /*pdfText(__dirname + "/doodle.pdf", function(err, chunks) {
            //console.log(chunks);
        });*/



        var pdf_path = __dirname + filename;

        //option to extract text from page 0 to 10 
        var option = {from: 0, to: 10};
        var pdf_body;

        pdfUtil.pdfToText(pdf_path, option, function(err, data) {
            pdf_body = data;  
            var result;

            try{
                var namez = /AUTHORIZED SIGNATURE:\s+(.+?)\s{2}/.exec(data)[1];
                var emailz = /Email: (.+?)\s{2}/.exec(data)[1];
                result = {query:query, info:{name: namez, email: emailz}};

            } catch (err){
                //result = {query:query, error:"non-selectable text in PDF"};
            }

            results.push(result);

            //console.log(JSON.stringify(result));
        });


    }
}

the library I am using is called request

and the documentation shows the following:

The callback argument gets 3 arguments:

 An error when applicable (usually from http.ClientRequest object) An http.IncomingMessage object The third is the response body (String or Buffer, or JSON object if the json option is supplied) 

The most straight forward approach would be to simply change the signature of getPDF to

function getPDF (filename, error, response, body) {...}

then you'd call it from within an anonymous function as the callback handler of request.post:

request.post({...}, function(error, response, body){
    getPDF( 'filename', error, response, body);
});

E6 notation makes this even nicer:

request.post({...}, (error, response, body)=>{
    getPDF( 'filename', ...arguments);
});

You can accomplish this using a more sophisticated method by partially applying the additional argument and then calling it in point-free fashion:

function getPDF (filename, error, response, body, ) {...}
getPdf2 = getPDF.bind(null, "filename"); 
request.post({...}, getPdf2);

The approaches amount to to the same thing and I might use either depending on circumstances. Arguably, the intent of the second approach is easier to read. It's certainly more fashionable.

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