0

I'm trying to get all the "tags" for each row of my table "labdesc". To do this I have created a get controller with this code:

getLabDescs: async (req, res, next) => {
    try {    
      const resRef = await verifyRefreshToken(req);
      const manager_id = resRef[2];
      getLabDescs(manager_id, async (err, results) => {
        if (err) { 
          return next(createError.InternalServerError())
        }
        results = results.map(item => {
          item.tag = await getTags("11",item.labourdesc_id,async (err1, res1) => {
            if (err1) { 
              return next(createError.InternalServerError())
            } 
            return res1       
          })
          console.log("here " +item.tag)
          return(item)
        });            
        return res.json({
          success: (results ? 1 : 0 ),
          message: results
        });     
      });
    } catch (error) {
      next(error)
    } 
  },

But the problem is that, as seen on console.log("here " + item.tag) statement, i just obtain a lot of "here [object Promise]". So I tried to put await in front of getTags controller but I have this error: "SyntaxError: await is only valid in async function".

Here the code of the getTags controller:

getTags: async (cod_table,record, callBack) => {
    const connection = await mysql.connection();
    let tagsArray = '';
    let results = [];
    try {     
      //console.log("at getTags...");
      tagsArray = await connection.query(
        `SELECT tag
        FROM s_com_tags 
        WHERE cod_table = ? AND id_record = ?`,
        [cod_table, record]
      );
      for(let i=0;i<tagsArray.length;i++){
        results.push(tagsArray[i].tag);
      }
    } catch (err) {
      throw err;
    } finally {
      await connection.release();
      return callBack(null, results);
    }     
  },

So i know that getTags is async, so I don't know why i have this error... What i'm doing wrong? thank you

July
  • 516
  • 1
  • 7
  • 25
  • 1
    You're using `await` within the function `results.map(item => {`, but it's not `async` – Nick Parsons Apr 21 '21 at 12:07
  • 1
    your `map` function is not async, change the line to `results = results.map(async item => {` – Kodiak Apr 21 '21 at 12:12
  • 1
    this is a bit of an async nightmare; i encourage to you to look into Promises, then learn asyn/await. You have async/await on a callback (not necessarily a bad thing, but in your above context, its confusing)...and it just seems like you're a bit all over the place with your solution. – LostJon Apr 21 '21 at 12:32
  • 1
    thank you guys for your answers!! you are amazing! and @LostJon i use promise in connection.query to return the results because i have other queries with insert and i have to do a insert in multiple tables (because of the secondary key relationships, since i use MySql model). So i use promise on query to handle connection.query("COMMIT"); and "ROLLBACK". But i definitely agree with you that i have to organize better the structure... thank you for you opinion – July Apr 21 '21 at 12:41
  • 1
    A great, humble response. I provided that opinion just because if someone else had to come into this code, its hard to follow; meaning there is a bit of tech debt. good luck! – LostJon Apr 21 '21 at 12:44

0 Answers0