Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert node.js mysql database result to json
#1
Hello guys..

I am trying to select data from Mysql Database and show it via JSON. Now I am getting the result via the console window of command prompt or via the powers h. All I want, get the result in JSON format. The code that I want to convert to json are the following.

Quote:app.get('/select', function(req,res){
   pool.getConnection(function(error,conn){
       var queryString = "SELECT * FROM `installation`";

       conn.query(queryString,function(error,results){
           if(error)
               {
                   throw error;
               }
           else
               {
                 console.log(results);
               }

       });
       conn.release();
   });
});


Please help me with this.
Reply
#2
(07-09-2017, 06:29 PM)waphunt@gmail.com Wrote: Hello guys..

I am trying to select data from Mysql Database and show it via JSON. Now I am getting the result via the console window of command prompt or via the powers h. All I want, get the result in JSON format. The code that I want to convert to json are the following.

Quote:app.get('/select', function(req,res){
   pool.getConnection(function(error,conn){
       var queryString = "SELECT * FROM `installation`";

       conn.query(queryString,function(error,results){
           if(error)
               {
                   throw error;
               }
           else
               {
                 console.log(results);
               }

       });
       conn.release();
   });
});


Please help me with this.

Instead of using the console.log(results); use the below code. Your problem will be solved.


Code:
res.end(JSON.stringify(results));


I hope this helped you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)