Export query Node.js to HTML

Asked

Viewed 22 times

-2

I’m starting in Node.js and I did a Crud, and I’m trying to return a query to my homepage, but I couldn’t find documentation to save this Count and take it there. (I preferred not to use the sequelize initially).

I can already have the value in the log.

app.get("/",function(req,res){

        sql.query({sql: 'SELECT COUNT(*) AS count FROM clientes where idstatus="Critico"'}, function (err, criticos) {
            console.log(criticos[0].count + ' Criticos');
            });

           sql.query({sql: 'SELECT COUNT(*) AS count FROM clientes where idstatus="Em Implantação"'}, function (err, emimp) {
           console.log(emimp[0].count + ' Em Implantação');
           });

          sql.query({sql: 'SELECT COUNT(*) AS count FROM clientes where idstatus="Paralisado"'}, function (err, paral) {
          console.log(paral[0].count + ' Pralisados');
           });

           sql.query({sql: 'SELECT COUNT(*) AS count FROM clientes where idstatus="Não iniciado"'}, function (err, naoini) {
           console.log(naoini[0].count + ' Não iniciados');
           });



    });

1 answer

1

You must fill out the Answer and send it to the results to be returned by the API.

Modify your code to look like this:

app.get("/",function(req,res){

var queryResults = [];

        sql.query({sql: 'SELECT COUNT(*) AS count FROM clientes where idstatus="Critico"'}, function (err, criticos) {
            console.log(criticos[0].count + ' Criticos');
            queryResults.push(criticos);
            });

           sql.query({sql: 'SELECT COUNT(*) AS count FROM clientes where idstatus="Em Implantação"'}, function (err, emimp) {
           console.log(emimp[0].count + ' Em Implantação');
            queryResults.push(emimp);

           });

          sql.query({sql: 'SELECT COUNT(*) AS count FROM clientes where idstatus="Paralisado"'}, function (err, paral) {
          console.log(paral[0].count + ' Pralisados');
            queryResults.push(paral);

           });

           sql.query({sql: 'SELECT COUNT(*) AS count FROM clientes where idstatus="Não iniciado"'}, function (err, naoini) {
           console.log(naoini[0].count + ' Não iniciados');
            queryResults.push(naoini);

           });

           res.send(queryResults);

    });

Browser other questions tagged

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