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