node.jsweb-scrapingnightmare

How to use a variable outside of its scope in node.js and nightmare.js (web scraping)


How to use a variable outside of its scope in node.js and nightmare.js (web scraping)

When i try to use the variable 'downloadLink' out of the await scope, it returns as undefined.

app.post('/search', function(req, res){
        const val = req.body.searchText;
        
        const nightmare = new Nightmare({ 
            show: true 
        });

        
        (async function() {

            const downloadLink = await nightmare
        
                .viewport(1200, 700)
                .goto('https://google.com/')
                .insert('#selector0')
                .click('#selector1')
                .click('#selector2')
                .evaluate(() => document.querySelector('#selector3').href)
                .end()
            
                .catch((err) => {
                    console.log(err)
                })
            console.log('download link ' + downloadLink) //this line prints a string
        
        })();

        console.log('download link ' + downloadLink) //this line returns undefined
    })

can i use 'downloadLink' outside of its scope and print it using the latter line of code??


Solution

  • Express supports async handlers, so you can refactor your method like this. No need to put the code under an async IIFE.

    app.post('/search', async function(req, res){
        try {
            const val = req.body.searchText;
        
            const nightmare = new Nightmare({ 
                show: true 
            });
            
            const downloadLink = await nightmare
                .viewport(1200, 700)
                .goto('https://google.com/')
                .insert('#selector0')
                .click('#selector1')
                .click('#selector2')
                .evaluate(() => document.querySelector('#selector3').href)
                .end()
            
    
            console.log('download link ' + downloadLink);
        } catch (err) {
            console.error(err.message);
        }
    });