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;
}
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
add a comment |
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
Note that because your function is declared with theasync
keyword, you don't need to explicitly return a promise withreturn Promise.resolve(dealArray)
. You can simplyreturn dealArray
and it will be wrapped in a promise due to theasync
keyword.
– chharvey
Jan 11 at 22:11
add a comment |
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
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
javascript node.js asynchronous puppeteer
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 theasync
keyword, you don't need to explicitly return a promise withreturn Promise.resolve(dealArray)
. You can simplyreturn dealArray
and it will be wrapped in a promise due to theasync
keyword.
– chharvey
Jan 11 at 22:11
add a comment |
Note that because your function is declared with theasync
keyword, you don't need to explicitly return a promise withreturn Promise.resolve(dealArray)
. You can simplyreturn dealArray
and it will be wrapped in a promise due to theasync
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
add a comment |
2 Answers
2
active
oldest
votes
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.
Seems like the last iteration gives a null value. I'm going to solve it myself.
– Alessandro Bertozzi
Jan 3 at 14:06
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Seems like the last iteration gives a null value. I'm going to solve it myself.
– Alessandro Bertozzi
Jan 3 at 14:06
add a comment |
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.
Seems like the last iteration gives a null value. I'm going to solve it myself.
– Alessandro Bertozzi
Jan 3 at 14:06
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 12 at 20:17
Alessandro BertozziAlessandro Bertozzi
2916
2916
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Note that because your function is declared with the
async
keyword, you don't need to explicitly return a promise withreturn Promise.resolve(dealArray)
. You can simplyreturn dealArray
and it will be wrapped in a promise due to theasync
keyword.– chharvey
Jan 11 at 22:11