Home > Software design >  Getting Async Await Error and blank value in Array
Getting Async Await Error and blank value in Array

Time:09-29

I am getting the below error:

SyntaxError: await is only valid in async function

Also I got the blank array "console.log(userTasklist)". Please provide some solution

const mysql = require('mysql2/promise');
    // create the connection to database
    const connection = async ()=> { 
        return await mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: '',
            database: 'whosin'
        })
    }
exports.allUserList = async (req, res) => {
    const db = await connection()
    let userID = req.params.userid;    
    
    const userdata = await db.query('SELECT name, user_id, employee_code FROM users WHERE under_gh = ?', [userID]);
    //console.log(userdata[0])
    
        if (userdata[0] && userdata[0].length > 0) {
            let userTasklist = [];
            userdata[0].map((datauser) => {
                var objtask = {};
                const taskdata =  await db.query("SELECT DATE_FORMAT(created_date, '%Y-%m-%d') as created_date, created_time, tasklist, user_id, DATE_FORMAT(created_by, '%Y-%m-%d') as date, DATE_FORMAT(created_by, '%H:%i:%s') as time FROM tasklist where user_id = ?", [datauser.user_id]);
               
                    if (taskdata[0] && taskdata[0].length > 0) {
                        objtask = {
                            userid: datauser.user_id,
                            tasklist: taskdata[0]
                        }
                        console.log(objtask);
                        userTasklist.push(objtask);
                    }
                
            })
            console.log(userTasklist)
            //res.send({ message: "user list fetched", userdata: userdata[0], tasklistdata: userTasklist })
        }   
}

CodePudding user response:

The scope of the wait for the db.query is within the map function. The map function needs to have the async keyword. However, it still will not work unless you wrap it in a Promise.all. This will make sure all the map iterations are resolved before moving forward in your code.

exports.allUserList = async (req, res) => {
  const db = await connection()
  let userID = req.params.userid

  const userdata = await db.query('SELECT name, user_id, employee_code FROM users WHERE under_gh = ?', [userID])
  // console.log(userdata[0])

  if (userdata[0] && userdata[0].length > 0) {
    let userTasklist = []
    await Promise.all(
      userdata[0].map(async (datauser) => {
        var objtask = {}
        const taskdata = await db.query("SELECT DATE_FORMAT(created_date, '%Y-%m-%d') as created_date, created_time, tasklist, user_id, DATE_FORMAT(created_by, '%Y-%m-%d') as date, DATE_FORMAT(created_by, '%H:%i:%s') as time FROM tasklist where user_id = ?", [datauser.user_id])

        if (taskdata[0] && taskdata[0].length > 0) {
          objtask = {
            userid: datauser.user_id,
            tasklist: taskdata[0]
          }
          console.log(objtask)
          userTasklist.push(objtask)
        }
      })
    )

    console.log(userTasklist)
    // res.send({ message: "user list fetched", userdata: userdata[0], tasklistdata: userTasklist })
  }
}

CodePudding user response:

You should change userdata[0].map((datauser) => { to userdata[0].map(async (datauser) => {

  • Related