Problems when extracting several jsons with queries in a nodejs module

0

I have a module in a route that needs to return a json with the content of several queries made in the same module and some of these queries are made with data extracted from the first queries, my code is similar to the following, but the problem I have it at the time of extracting the data, neither with callback nor with promise I managed to extract sincrona of the values of the queries to be able to return the json composed of the queries, in some sites they answer with what is impossible, I think I'm green and must be a bullshit, I do not think that in a module can not return a array/json/string with a compound of several queries to database, please someone can help I've been several days and several methods and I can not get it to work in any way.

My objective in a rough way would be the following:

var sql_ejecutar ="select * from tabla_generica";
              var json1=  conexion.query(sql_ejecutar, function(err, results, fields)
                    {
                        //Contenido en results, devue   
                    }); 


    conexion.end();
              var json2=  conexion.query(sql_ejecutar, function(err, results, fields)
                    {
                        //Contenido en results, devue   
                    }); 


    conexion.end();
              var json3=  conexion.query(sql_ejecutar, function(err, results, fields)
                    {
                        //Contenido en results, devue   
                    }); 


conexion.end();
var json_devolver[0]['json1']=json1;
var json_devolver[0]['json2']=json2;
var json_devolver[0]['json3']=json3;
res.json(json_devolver);

How to do it correctly within the same NodeJS module?

    
asked by Rafael Lozano Ruiz 17.05.2017 в 23:57
source

2 answers

0

It is an asynchronous function, to return the data you have to receive it in another asynchronous function (callbacks). Something like

function procesar3sql(){
    var sql_ejecutar ="select * from tabla_generica";
    conexion.query(sql_ejecutar, function(err, result1, field1){
        conexion.query(sql_ejecutar, function(err, result2, field2){
            conexion.query(sql_ejecutar, function(err, result3, fields){
                res.json({json1: result1, json2: result2, json3: result3});
                conexion.end();
            });
        });
    }); 
}
procesar3sql();
    
answered by 18.05.2017 в 01:07
0

Since it is asynchronous, you must return the result in the callback. What does this mean?

If you have a function that performs an asynchronous operation, in this case a mysql query, it should be like the following example:

function consultar (params = {}, callback) {
  conexion.query(sql, function (err, res, fields) {
    if (err) return callback(err)
    callback(null, res, fields)
  })
}

Where callback is a function, that is, you pass by a function a function that will be the callback at the time you invoke it like this:

consultar({id: 1}, function(err, res, fields) {
  if (err) throw err
  console.log(res)
  console.log(fields)
})

Do you see the callback? The callback variable that you pass as the second parameter in the query function is the function you are using to obtain the result. Another observation is that if the query generates error, the callback will only receive the error and no fields or fields, but if there is no error, then you will pass null, res and fields, that is, if there is no error you pass a null.

Once this is explained, one way to solve your problem is something like the following:

function consultar3Querys (params = {}, callback) {
  conexion.query(sql, function (err, res, fields) {
    if (err) return callback(err)
    conexion.query(sql, function (err, res, fields) {
      if (err) return callback(err)
      conexion.query(sql, function (err, res, fields) {
        if (err) return callback(err)
        callback(res, fields)
      })
    })
  })
}


consultar({id: 1}, function(err, res, fields) {
  if (err) throw err
  var datos = { dato1: res.dato1, dato2: res.dato2, datoN: res.datoN}
})

This works, but it is a bad practice and it is known as callback hell so it is advisable to use promises.

    
answered by 19.05.2017 в 00:55