Delay in response to MYSQL query

Asked

Viewed 49 times

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, the Mysql.getConnection shouldn’t be a await before?

  • 1

    Vagner tried to put the await but it wasn’t when I implemented the callback worked. Thanks!

1 answer

1


I implemented the callback and it was solved.

import { Request, response, Response } from 'express'

const Mysql = require('../../database/MsqlConnection').pool;

class MsqlRequests {

  public static Ramais(callback: (array) => void) {

    var ResultadoJSON
    const 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)
                })
                return callback(array);
            }
        )
    })
  }
}


export default MsqlRequests

Calling for:

var array;

export class Controller {

public async efetuarReducao(req: Request, res: Response): Promise<void> {

var obj = {
    relatorio1: [],
    relatorioRamais: []
};

    array = MsqlRequests.Ramais(function (array2) {

        array2.map(function (item) {
            obj.relatorioRamais.push(item)
        })
        obj.relatorioRamais.map(OndeRealizoAsCondicoes);

        res.send(obj)

    }

function OndeRealizoAsCondicoes(elemento) {

/*Realizo condicoes */

}
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.