async method always returning undefined





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I can't solve that problem so I'm asking that here:



This is the async function, and as you can see is returning an array. But it returns an undefined value.



async function scrape(pageURL) {
var dealArray = ;
try {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(pageURL);
await page.waitForSelector('div.s-item-container');
const dealsElements = await page.$$('div.s-item-container');
for(deal of dealsElements) {
let dealTitleElement = await deal.$('div.s-item-container a.s-access-detail-page');
let dealTitleValue = await (await dealTitleElement.getProperty('title')).jsonValue();
let dealPriceElement= await deal.$('div.s-item-container span.a-color-price');
let dealPriceValue = await (await dealPriceElement.getProperty('textContent')).jsonValue();
let dealReviewsElement = await deal.$('div.s-item-container .a-icon-star');
let dealLinkValue = await (await dealTitleElement.getProperty('href')).jsonValue() + '&tag=dragonstv-21';
let dealReviewsClass = await (await dealReviewsElement.getProperty('className')).jsonValue();
let dealReviewsValue;
if(dealReviewsClass) {
let starValue = dealReviewsClass.substring(26);
if(starValue.indexOf('-') === -1) {
dealReviewsValue = starValue;
} else {
let stars = starValue.replace('-', '.');
dealReviewsValue = stars;
}
}
dealArray.push({
"title": dealTitleValue,
"price": dealPriceValue,
"reviews": dealReviewsValue + "/5.0",
"link": dealLinkValue,
"store": "Amazon",
});
}
return Promise.resolve(dealArray);
} catch(e) {
console.error('Error: ' + e);
}
}


And here is how I'm calling it:



scrape('working link').then((data) => {
console.log(data) // result: undefined
}


It works only if I declare the variable out of the function and the function doesn't return anything but only changes the array content.










share|improve this question

























  • Note that because your function is declared with the async keyword, you don't need to explicitly return a promise with return Promise.resolve(dealArray). You can simply return dealArray and it will be wrapped in a promise due to the async keyword.

    – chharvey
    Jan 11 at 22:11


















0















I can't solve that problem so I'm asking that here:



This is the async function, and as you can see is returning an array. But it returns an undefined value.



async function scrape(pageURL) {
var dealArray = ;
try {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(pageURL);
await page.waitForSelector('div.s-item-container');
const dealsElements = await page.$$('div.s-item-container');
for(deal of dealsElements) {
let dealTitleElement = await deal.$('div.s-item-container a.s-access-detail-page');
let dealTitleValue = await (await dealTitleElement.getProperty('title')).jsonValue();
let dealPriceElement= await deal.$('div.s-item-container span.a-color-price');
let dealPriceValue = await (await dealPriceElement.getProperty('textContent')).jsonValue();
let dealReviewsElement = await deal.$('div.s-item-container .a-icon-star');
let dealLinkValue = await (await dealTitleElement.getProperty('href')).jsonValue() + '&tag=dragonstv-21';
let dealReviewsClass = await (await dealReviewsElement.getProperty('className')).jsonValue();
let dealReviewsValue;
if(dealReviewsClass) {
let starValue = dealReviewsClass.substring(26);
if(starValue.indexOf('-') === -1) {
dealReviewsValue = starValue;
} else {
let stars = starValue.replace('-', '.');
dealReviewsValue = stars;
}
}
dealArray.push({
"title": dealTitleValue,
"price": dealPriceValue,
"reviews": dealReviewsValue + "/5.0",
"link": dealLinkValue,
"store": "Amazon",
});
}
return Promise.resolve(dealArray);
} catch(e) {
console.error('Error: ' + e);
}
}


And here is how I'm calling it:



scrape('working link').then((data) => {
console.log(data) // result: undefined
}


It works only if I declare the variable out of the function and the function doesn't return anything but only changes the array content.










share|improve this question

























  • Note that because your function is declared with the async keyword, you don't need to explicitly return a promise with return Promise.resolve(dealArray). You can simply return dealArray and it will be wrapped in a promise due to the async keyword.

    – chharvey
    Jan 11 at 22:11














0












0








0








I can't solve that problem so I'm asking that here:



This is the async function, and as you can see is returning an array. But it returns an undefined value.



async function scrape(pageURL) {
var dealArray = ;
try {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(pageURL);
await page.waitForSelector('div.s-item-container');
const dealsElements = await page.$$('div.s-item-container');
for(deal of dealsElements) {
let dealTitleElement = await deal.$('div.s-item-container a.s-access-detail-page');
let dealTitleValue = await (await dealTitleElement.getProperty('title')).jsonValue();
let dealPriceElement= await deal.$('div.s-item-container span.a-color-price');
let dealPriceValue = await (await dealPriceElement.getProperty('textContent')).jsonValue();
let dealReviewsElement = await deal.$('div.s-item-container .a-icon-star');
let dealLinkValue = await (await dealTitleElement.getProperty('href')).jsonValue() + '&tag=dragonstv-21';
let dealReviewsClass = await (await dealReviewsElement.getProperty('className')).jsonValue();
let dealReviewsValue;
if(dealReviewsClass) {
let starValue = dealReviewsClass.substring(26);
if(starValue.indexOf('-') === -1) {
dealReviewsValue = starValue;
} else {
let stars = starValue.replace('-', '.');
dealReviewsValue = stars;
}
}
dealArray.push({
"title": dealTitleValue,
"price": dealPriceValue,
"reviews": dealReviewsValue + "/5.0",
"link": dealLinkValue,
"store": "Amazon",
});
}
return Promise.resolve(dealArray);
} catch(e) {
console.error('Error: ' + e);
}
}


And here is how I'm calling it:



scrape('working link').then((data) => {
console.log(data) // result: undefined
}


It works only if I declare the variable out of the function and the function doesn't return anything but only changes the array content.










share|improve this question
















I can't solve that problem so I'm asking that here:



This is the async function, and as you can see is returning an array. But it returns an undefined value.



async function scrape(pageURL) {
var dealArray = ;
try {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(pageURL);
await page.waitForSelector('div.s-item-container');
const dealsElements = await page.$$('div.s-item-container');
for(deal of dealsElements) {
let dealTitleElement = await deal.$('div.s-item-container a.s-access-detail-page');
let dealTitleValue = await (await dealTitleElement.getProperty('title')).jsonValue();
let dealPriceElement= await deal.$('div.s-item-container span.a-color-price');
let dealPriceValue = await (await dealPriceElement.getProperty('textContent')).jsonValue();
let dealReviewsElement = await deal.$('div.s-item-container .a-icon-star');
let dealLinkValue = await (await dealTitleElement.getProperty('href')).jsonValue() + '&tag=dragonstv-21';
let dealReviewsClass = await (await dealReviewsElement.getProperty('className')).jsonValue();
let dealReviewsValue;
if(dealReviewsClass) {
let starValue = dealReviewsClass.substring(26);
if(starValue.indexOf('-') === -1) {
dealReviewsValue = starValue;
} else {
let stars = starValue.replace('-', '.');
dealReviewsValue = stars;
}
}
dealArray.push({
"title": dealTitleValue,
"price": dealPriceValue,
"reviews": dealReviewsValue + "/5.0",
"link": dealLinkValue,
"store": "Amazon",
});
}
return Promise.resolve(dealArray);
} catch(e) {
console.error('Error: ' + e);
}
}


And here is how I'm calling it:



scrape('working link').then((data) => {
console.log(data) // result: undefined
}


It works only if I declare the variable out of the function and the function doesn't return anything but only changes the array content.







javascript node.js asynchronous puppeteer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 13:52









Elliot Nelson

8,93532136




8,93532136










asked Jan 3 at 12:29









Alessandro BertozziAlessandro Bertozzi

2916




2916













  • Note that because your function is declared with the async keyword, you don't need to explicitly return a promise with return Promise.resolve(dealArray). You can simply return dealArray and it will be wrapped in a promise due to the async keyword.

    – chharvey
    Jan 11 at 22:11



















  • Note that because your function is declared with the async keyword, you don't need to explicitly return a promise with return Promise.resolve(dealArray). You can simply return dealArray and it will be wrapped in a promise due to the async keyword.

    – chharvey
    Jan 11 at 22:11

















Note that because your function is declared with the async keyword, you don't need to explicitly return a promise with return Promise.resolve(dealArray). You can simply return dealArray and it will be wrapped in a promise due to the async keyword.

– chharvey
Jan 11 at 22:11





Note that because your function is declared with the async keyword, you don't need to explicitly return a promise with return Promise.resolve(dealArray). You can simply return dealArray and it will be wrapped in a promise due to the async keyword.

– chharvey
Jan 11 at 22:11












2 Answers
2






active

oldest

votes


















1














As written, your function must return an array (empty or otherwise). If it's returning undefined, then you're generating an exception and should see one in the console, via your catch statement. If you're not seeing it, you might try removing the try/catch and see what exception bubbles up.






share|improve this answer
























  • Seems like the last iteration gives a null value. I'm going to solve it myself.

    – Alessandro Bertozzi
    Jan 3 at 14:06



















0














I've actually figured the problem out. It was returning a string so I had to use JSON.parse(request) so I have an object on which I can work on.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022328%2fasync-method-always-returning-undefined%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As written, your function must return an array (empty or otherwise). If it's returning undefined, then you're generating an exception and should see one in the console, via your catch statement. If you're not seeing it, you might try removing the try/catch and see what exception bubbles up.






    share|improve this answer
























    • Seems like the last iteration gives a null value. I'm going to solve it myself.

      – Alessandro Bertozzi
      Jan 3 at 14:06
















    1














    As written, your function must return an array (empty or otherwise). If it's returning undefined, then you're generating an exception and should see one in the console, via your catch statement. If you're not seeing it, you might try removing the try/catch and see what exception bubbles up.






    share|improve this answer
























    • Seems like the last iteration gives a null value. I'm going to solve it myself.

      – Alessandro Bertozzi
      Jan 3 at 14:06














    1












    1








    1







    As written, your function must return an array (empty or otherwise). If it's returning undefined, then you're generating an exception and should see one in the console, via your catch statement. If you're not seeing it, you might try removing the try/catch and see what exception bubbles up.






    share|improve this answer













    As written, your function must return an array (empty or otherwise). If it's returning undefined, then you're generating an exception and should see one in the console, via your catch statement. If you're not seeing it, you might try removing the try/catch and see what exception bubbles up.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 3 at 13:54









    Elliot NelsonElliot Nelson

    8,93532136




    8,93532136













    • Seems like the last iteration gives a null value. I'm going to solve it myself.

      – Alessandro Bertozzi
      Jan 3 at 14:06



















    • Seems like the last iteration gives a null value. I'm going to solve it myself.

      – Alessandro Bertozzi
      Jan 3 at 14:06

















    Seems like the last iteration gives a null value. I'm going to solve it myself.

    – Alessandro Bertozzi
    Jan 3 at 14:06





    Seems like the last iteration gives a null value. I'm going to solve it myself.

    – Alessandro Bertozzi
    Jan 3 at 14:06













    0














    I've actually figured the problem out. It was returning a string so I had to use JSON.parse(request) so I have an object on which I can work on.






    share|improve this answer




























      0














      I've actually figured the problem out. It was returning a string so I had to use JSON.parse(request) so I have an object on which I can work on.






      share|improve this answer


























        0












        0








        0







        I've actually figured the problem out. It was returning a string so I had to use JSON.parse(request) so I have an object on which I can work on.






        share|improve this answer













        I've actually figured the problem out. It was returning a string so I had to use JSON.parse(request) so I have an object on which I can work on.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 12 at 20:17









        Alessandro BertozziAlessandro Bertozzi

        2916




        2916






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022328%2fasync-method-always-returning-undefined%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$