Home > Enterprise >  For loop - SyntaxError: await is only valid in async function
For loop - SyntaxError: await is only valid in async function

Time:09-24

I'm fairly new to NodeJS and I'm struggling a bit to get my head around what's happening here and why I'm getting the error: "SyntaxError: await is only valid in async function" for await doScrape(results[i].productUrl);

I've looked at other examples but still can't spot what's wrong. Any help greatly appreciated. Thanks.

const mysql = require('mysql');
const axios = require('axios');

async function doScrape(url) {
  try {
    console.log('scraping');
    const response = await axios.get('https://###########.com/dev?url='   url);
    console.log(response.status);
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

module.exports.dailyScraperScheduler = async (event) => {

  var con = mysql.createConnection({
    host: "#####",
    user: "#####",
    password: "#####",
    database: "####"
  });

  let sql = 'SELECT productUrl FROM ######### ppw INNER JOIN products \
             p ON p.productId = ppw.productId WHERE IFNULL(ppw.lastUpdated,CURDATE() \
             - INTERVAL 1 DAY) < CURDATE()' 

  let getProducts = async (sql, params,results) => {
    return new Promise((resolve, reject) => {
        con.connect((err, connection) => {
            con.query(sql, params, (err, results) => {
                if (err){
                    reject(err);
                }
                resolve(results);
            });
        });
    });
  };
  
  await getProducts(sql)
  .then(function (results) {
    for (var i = 0; i < results.length; i  ) {
      console.log(results[i].productUrl);
      await doScrape(results[i].productUrl);
    };
  });
};

CodePudding user response:

you are using await without the async. in the end of your code.

await getProducts(sql)
  .then(function (results) {
    for (var i = 0; i < results.length; i  ) {
      console.log(results[i].productUrl);
      await doScrape(results[i].productUrl);
    };
  });

replace by

await getProducts(sql)
  .then(async function (results) {
    for (var i = 0; i < results.length; i  ) {
      console.log(results[i].productUrl);
      await doScrape(results[i].productUrl);
    };
  });

CodePudding user response:

You must need to add async on the function.

const mysql = require('mysql');
const axios = require('axios');

async function doScrape(url) {
  try {
    console.log('scraping');
    const response = await axios.get('https://###########.com/dev?url='   url);
    console.log(response.status);
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

module.exports.dailyScraperScheduler = async (event) => {

  var con = mysql.createConnection({
    host: "#####",
    user: "#####",
    password: "#####",
    database: "####"
  });

  let sql = 'SELECT productUrl FROM ######### ppw INNER JOIN products \
             p ON p.productId = ppw.productId WHERE IFNULL(ppw.lastUpdated,CURDATE() \
             - INTERVAL 1 DAY) < CURDATE()' 

  let getProducts = async (sql, params,results) => {
    return new Promise((resolve, reject) => {
        con.connect((err, connection) => {
            con.query(sql, params, (err, results) => {
                if (err){
                    reject(err);
                }
                resolve(results);
            });
        });
    });
  };
  
  await getProducts(sql)
  .then(async function (results) {
    for (var i = 0; i < results.length; i  ) {
      console.log(results[i].productUrl);
      await doScrape(results[i].productUrl);
    };
  });
};
  • Related