0
I have a problem that I seek solution but believe that I may not be implementing correctly. I have a query to mysql that returns me extensions, this function should return me to another class so saving in a variable. But the answer takes me returning so it returns the empty value only after the conditions it should do with this value it returns the value of the query. I believe an async/await setting should be made but I couldn’t do it properly. It is a Nodejs backend server using typescript. Could you help me?
Query to the database:
class MsqlRequests {
public static Ramais()
{
var ResultadoJSON
var array = []
Mysql.getConnection((error, conn) => {
if (error) { return response.status(500).send({ error: error }) }
conn.query(
'SELECT ativo, ramal FROM ramais;',
(error, resultado, fields) => {
conn.release();
if (error) { return response.status(500).send({ error: error }) }
ResultadoJSON = JSON.parse(JSON.stringify(resultado));
ResultadoJSON.map(function(item){
array.push(item)
})
console.log("array 1")
console.log(array)
return array
}
)
})
console.log("array 2")
console.log(array)
return array
}
Where I make the call:
export class Controller {
public async efetuarReducao(req: Request, res: Response): Promise<void> {
var obj = {
relatorio1: [],
relatorioRamais: []
};
console.log("Ramais da função")
obj.relatorio1.push(MsqlRequests.Ramais()) // Chamo a função de consulta MSQL e preciso salvar em obj.relatorio1
obj.relatorio.map(reduzir);
function OndeRealizoAsCondicoes(elemento) {
}
res.json(obj)
}
}
In class
Ramais
, theMysql.getConnection
shouldn’t be aawait
before?– Vagner Wentz
Vagner tried to put the await but it wasn’t when I implemented the callback worked. Thanks!
– Eduardo Berzotti