0

the function getQAfromAmazon(productID) return value in productInfo. but for this line:

const product = await getQAfromAmazon(productID);
console.log("product is:" ,  product);

the result is: product is: undefined

how can I insert the result to product?

const express = require('express');
const app = express();
const puppeteer = require('puppeteer');
const fs = require('fs');

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*"); //for public api
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use(express.urlencoded({ extended: false }));
app.use(express.json())
app.use(express.static('../client'));

app.post('/getQuestionsAndAnswers', async(req, res) => {
  console.log("req.body.productId", req.body.productId);

  let productID = req.body.productId;
  const product = await getQAfromAmazon(productID);
  console.log("product is:", product);
});

app.listen(3000);

async function getQAfromAmazon(productID) {
  puppeteer.launch({
      headless: true,
      slowMo: 250,
      args: ['--no-sandbox', '--disable-setuid-sandbox', '--window-size=1920,1080', '--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3312.0 Safari/537.36"']
    })
    .then(async browser => {
      const page = await browser.newPage();
      console.log("go to page");

      await page.goto("https://www.amazon.com/ask/questions/asin/" + productID);
      console.log("waiting for selector body");

      await page.waitForSelector('body');

      var productInfo = await page.evaluate(() => {
        let questionsArray = [];
        let answersArray = [];

        console.log("waiting for selector question");

        let len = document.querySelectorAll("[id^='question']").length;
        console.log("len is: ", len);

        let str;
        let ans;

        for (let i = 1; i < len; i++) {
          let question = document.querySelectorAll("div.a-fixed-left-grid-col.a-col-right > a > span")[i - 1].innerText;
          console.log("question is: ", question);

          str = "#a-page > div.a-section.askQuestionListPage > div:nth-child(7) > div > div > div:nth-child(" + i + ") > div > div.a-fixed-left-grid-col.a-col-right > div.a-fixed-left-grid.a-spacing-base > div > div.a-fixed-left-grid-col.a-col-right > span:nth-child(3)";

          if (document.querySelector(str) == null) {
            ans = "no answer";
          } else {
            ans = document.querySelector(str).innerText;
          }

          questionsArray.push(question);
          answersArray.push(ans);
        };

        var product = {
          "questionsArray": questionsArray,
          "answersArray": answersArray
        };

        return product;
      });

      await browser.close();
      console.log("productInfo is:", productInfo);

      return productInfo;
    }).catch(function(error) {
      console.error(error);
    });
}

1
  • 1
    getQAfromAmazon() doesn't return anything (-> undefined) Commented Jul 11, 2020 at 11:43

1 Answer 1

3

Your getQAfromAmazon() has no return values. return productInfo; is called within puppeteer.launch(...).then(...).

Try something like this:

...
async function getQAfromAmazon(productID) {
  return puppeteer.launch({
    ...

So that getQAfromAmazon will return the promise.

Sign up to request clarification or add additional context in comments.

1 Comment

Or use async await to reduce nesting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.