bernardro / actor-youtube-scraper

Apify actor to scrape Youtube search results. You can set the maximum videos to scrape per page as well as the date from which to start scraping.
https://apify.com/bernardo/youtube-scraper
Apache License 2.0
23 stars 19 forks source link

await is only valid in async function #30

Closed windowsales closed 3 years ago

windowsales commented 3 years ago

Your example "const run = await Apify.call('bernardo/youtube-scraper', input);" produces an error " await is only valid in async function"

windowsales commented 3 years ago

Hi there, in the README, it would be nice to include a runnable node example. Here is how I got it working:

// This is the main Node.js source code file of your actor.

// Import Apify SDK. For more information, see https://sdk.apify.com/
const Apify = require('apify');

Apify.main(async () => {

// Prepare actor input
    /**
     * Actor code
     */
    const input = {
        postsFromDate: "5 years",
        searchKeywords: "bitcoin",
        extendOutputFunction: async ({ data, item, page, request, customData }) => {
            return item;
        },
        extendScraperFunction: async ({ page, request, requestQueue, customData, Apify, extendOutputFunction }) => {

        },
        customData: {},
        proxyConfiguration: {
            useApifyProxy: true
        }
    };

// Run the actor
    const run = await Apify.call('bernardo/youtube-scraper', input,{'token':'<YOUR TOKEN>'});

// Print actor output (if any)
    console.log('Output');
    console.dir(run.output);

// Fetch and print actor results from the run's dataset (if any)
    console.log('Results from dataset');
    const dataset = await Apify.openDataset(run.defaultDatasetId, { forceCloud: true });
    await dataset.forEach(async (item, index) => {
        console.log(JSON.stringify(item));
    });

});
pocesar commented 3 years ago

make sure you use await inside an async function, like this:

(async () => {
   await ...
})()

top-level await is a feature that is only available in Node 14+