1

I am trying to print JSON response in my local browser of the data from my local system


I have my code::

var express=require('express');
var fs=require('fs');
var http=require('http');
var crypto=require('crypto');
var mysql=require('mysql');
var async=require('async');

var app=express();

var connection=mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'ImagePostingDB'
});

connection.connect();
app.set('port',process.env.PORT||7002); 
app.use('/Details',express.static(__dirname+'/public/images')); 
app.use(express.bodyParser());



app.get('/DescriptionSortedPrice/',function(request,response){
  var name_of_restaurants;
  async.series( [
    // Get the first table contents
    function ( callback ) {
      connection.query('SELECT * FROM ImagePostingtable ORDER BY Sl_no', function(err, rows, fields)
      {
        console.log('Connection result error '+err);
        name_of_restaurants = rows;
        callback();
      });

    }
   // Send the response
   ], function ( error, results ) {
    response.json({
      'restaurants' : name_of_restaurants
    });
  });
});



http.createServer(app).listen(app.get('port'),function(){
  console.log('Express server listening on port'+app.get('port'));
});

When i tried with postman:: i have no JSON response::

enter image description here


  • How to resolve this ?

or

  • how to find my root cause of the problem

{Edit}

var express=require('express');
var fs=require('fs');
var http=require('http');
var crypto=require('crypto');
var mysql=require('mysql');
var async=require('async');

var app=express();

var connection=mysql.createConnection({
    host: 'localhost',
    user: 'root',
    database: 'ImagePostingDB'
});

connection.connect(function(err) { if ( !err ) { console.log("Connected to MySQL"); } else if ( err ) { console.log(err); } });
app.set('port',process.env.PORT||7002); 
app.use('/Details',express.static(__dirname+'/public/images')); 
app.use(express.bodyParser());





app.get('/DescriptionSortedPrice/',function(request,response){
  connection.query('SELECT * FROM ImagePostingtable ORDER BY Sl_no', function(err, rows, fields) {
    if (err) {
      return response.send(500, err.message);
    }

    response.json({
      'restaurants' : rows
    });
  });
});






http.createServer(app).listen(app.get('port'),function(){
        console.log('Express server listening on port'+app.get('port'));

});

I have a Snapshot in my command prompt like this::

enter image description here

Devrath
  • 42,072
  • 54
  • 195
  • 297
  • could you add: `connection.connect(function(err) { if ( !err ) { console.log("Connected to MySQL"); } else if ( err ) { console.log(err); } });` and tell us the output? – Jacob A. Dec 16 '13 at 10:35
  • sry i mean to add only this: `function(err) { if ( !err ) { console.log("Connected to MySQL"); } else if ( err ) { console.log(err); } }` in your `connection.connect()` function – Jacob A. Dec 16 '13 at 10:41
  • Not like that - he means modify your previous connection.connect – C Blanchard Dec 16 '13 at 10:42
  • @Igoel .... and @C Blanchard ..... Please look at the modified edit ...! – Devrath Dec 16 '13 at 10:46
  • Looks like the `connect` method is called twice… Doesn't look right to me. – Paul Mougel Dec 16 '13 at 11:09
  • @Paul Mougel ..... How can i resolve this(Clearly i have invoked conncet only once)... should i need to remove any modules ? ... Im new to express ... please guide me on identifying the root cause of the problem here – Devrath Dec 16 '13 at 11:13
  • 1
    No need to call `.connect()`, as it is implicitly called if needed ([see source](https://github.com/felixge/node-mysql/blob/v2.0.0-rc2/lib/Connection.js#L129)). – Paul Mougel Dec 16 '13 at 11:16
  • @Paul Mougel ....... Errors I am getting on the prompt are just deprication errors .... I came accross this ...Have a look at this link ....... http://stackoverflow.com/a/19611997/1083093 – Devrath Dec 16 '13 at 11:29
  • No they are not: the `Cannot enqueue Handshake after...` is a MySQL database, indicating that you are connecting to it twice. – Paul Mougel Dec 16 '13 at 11:57

1 Answers1

1

First: can you confirm that the server doesn't crash when you launch the request? A response code of 0 usually means that the connection was cut (or that no response was sent at all).

async.series is supposed to be used to call multiple asynchronous functions in series: there's no need to use it if you only call one function.

The callbacks in async.series follow Node.js's callback style, which means they take an error (or null) as a first parameter, and then the results of the function. But you don't send it any arguments: callback(). However, your code still works because you use a local variable name_of_restaurants that you overwrite in your asynchronous function. Be careful because that's not how you are supposed to use this pattern. Here is a more elegant solution:

app.get('/DescriptionSortedPrice/',function(request,response){
  connection.query('SELECT * FROM ImagePostingtable ORDER BY Sl_no', function(err, rows, fields) {
    if (err) {
      console.log('Encountered an error:', err.message);
      return response.send(500, err.message);
    }

    console.log('Found results:', rows);
    response.json({
      'restaurants' : rows
    });
  });
});
Paul Mougel
  • 16,728
  • 6
  • 57
  • 64
  • Thanks for correcting my design ....Please can you look at the edit...It shows i am connected to SQL ... by still i am not able to get JSON-response .... Any inputs on this .... or should i need to add any debugging lines to find the problem ? – Devrath Dec 16 '13 at 10:52
  • Using my code sample, does the server send back a response? If yes, with which status code? – Paul Mougel Dec 16 '13 at 10:59
  • I didn't get the response ... u can see in my edit ... i have applied your input .... in postman ... i still get the response as "The response status was 0." ! – Devrath Dec 16 '13 at 11:02
  • What does `console.log(err, rows)` output? – Paul Mougel Dec 16 '13 at 11:17