Sunday, 29 September 2019

How to use my existing funciton global.parseData(id, name, url) in `await page.evaluate(indexTotal => {` in Puppeteer?

SITUATION:

I was able to use the variable indexTotal inside the page.evaluate context like this:

var indexTotal = ...
(do something with indexTotal)

const thedata = await page.evaluate(indexTotal => {

 }, indexTotal).catch( error => {
                        console.log("ERROR: "+error);
                    });

PROBLEM:

But when it comes to global.parseData, I get the following error:

Unexpected token .

for this code:

const thedata = await page.evaluate((global.parseData(id,name,url)) => {

 }, (global.parseData(id,name,url))).catch( error => {
                        console.log("ERROR: "+error);
                    });

QUESTION:

What is the proper syntax to use my global function inside page.evaluate ?


EDIT:

getData.js

    getData: function() {

            let getData = async () => {
                url = 'url';

                await page.exposeFunction('parseData', (items2, items3, item2, ...) => global.parseData(items2, items3, item2, ...));

                const thedata = await page.evaluate( async() => {

                    var items = [];
                    var items2 = [];
                    var items3 = [];

                    $('.htmlElement').find('.childElements').each(function(ind) {
                        ...

                        var returnedObject = await window.parseData(items2, items3, item2, ...);

                        items.push(returnedObject.item);
                        items2 = returnedObject.items2;
                        items3 = returnedObject.items3;

                    });

                    var largeObject = {
                        items: items,
                        items2: items2,
                        items3: items3
                    }

                    console.log("RETURNED OBJECT: "+JSON.stringify(returnedObject));

                    return largeObject;

                }).catch( error => {
                    console.log("ERROR: "+error);
                });

                browser.close();
                return thedata;
            }

            getData().then(largeObject => {

                global.saveData(largeObject);

            });
    }

app.js

global.parseData = function(items2, items3, item2, ...) {

    ...


    if(!item2 || item2 == "") {
        ...
    }
    else {
        items2.push(item2);
        var item3 = ...;
        items3.push(item3);
    }

    var item = {
        ...
    }

    var largeObject = {
        item: item,
        items2: items2,
        items3: items3
    }

    console.log("LARGE OBJECT: "+JSON.stringify(largeObject));

    return largeObject;
}

CONSOLE.LOG OUTPUT:

RETURNED OBJECT: {}
LARGE OBJECT: {data}
RETURNED OBJECT: {}
RETURNED OBJECT: {}
RETURNED OBJECT: {}


from How to use my existing funciton global.parseData(id, name, url) in `await page.evaluate(indexTotal => {` in Puppeteer?

No comments:

Post a Comment