0

I have express server code below


  • I want to initiate an insert statement by getting the param value
  • Then inserting that param value into Database

What i have done so far is that i have learnt how to make JSON response ::

var express = require('express')
  , async = require('async')
  , http = require('http')
  , mysql = require('mysql');

var app = express();

var connection = mysql.createConnection({
    host: 'localhost',
    user: '*****',
    password: "*****",
    database: 'DB-NAME'
});

connection.connect();

// all environments
app.set('port', process.env.PORT || 1234);

app.use(express.static(__dirname + '/public/images'));


app.get('/Name/',function(request,response,next){

    var keyName=request.query.Key;
    var name_of_restaurants;
    async.series( [
        // Get the first table contents
        function ( callback ) {

           connection.query('SELECT * FROM RestaurantDescription where RestaurantName = ?', [keyName], 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'));
});

What i am trying to do::

  • I am trying to find how to make a insert statement
  • So that i could extract the Param value and insert that data to database
  • How to modify the above code to achieve my goal

Hope i am clear !


[EDIT]

var express = require('express')
  , async = require('async')
  , http = require('http')
  , mysql = require('mysql');

var app = express();

var connection = mysql.createConnection({
    host: 'localhost',
    user: '*********',
    password: "*********",
    database: 'DB_NAME'
});

connection.connect();

// all environments
app.set('port', process.env.PORT || 7000);

app.use(express.static(__dirname + '/public/images'));


app.get('/Name/',function(request,response,next){

    var keyName=request.query.Key;
    var name_of_restaurants;
    async.series( [

        function(callback) {

           connection.query('INSERT INTO RestaurantDescription (RestaurantName) VALUES (?)', [keyName], function (err, rows, fields) 
               {
                       console.log('Connection result error ' + err);        
                       callback();
               });
        }

   // Send the response
] );
} );


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
Devrath
  • 37,389
  • 47
  • 165
  • 245

1 Answers1

2

I'm not familiar with the libraries which you use but I think that you should add another function inside the array passed to async.series. The body of the function should have similar content as the one above.

async.series( [
  // Get the first table contents
  function(callback) {
    connection.query('SELECT * FROM RestaurantDescription where RestaurantName = ?', 
      [keyName], 
      function (err, rows, fields) {
        console.log('Connection result error ' + err);
        name_of_restaurants = rows;
        callback();
      }
    );
  },
  // inserting a value
  function(callback) {
    connection.query('INSERT INTO RestaurantDescription (RestaurantName) VALUES (?)', 
      [keyName], 
      function (err, rows, fields) {
        console.log('Connection result error ' + err);        
        callback();
      }
    );
  }
]

What will happen is that the both function will be executed asynchronous and at the end you will still send a response to the browser.

You are already getting a GET parameter via var keyName=request.query.Key;.

If you plan to use POST parameters, instead of

var keyName=request.query.Key;

You should add a middleware which parses the variables:

app.use(express.bodyParser());

And then

var keyName = request.body.Key;

I'm referring this comment https://stackoverflow.com/a/18167056/642670

Community
  • 1
  • 1
Krasimir
  • 12,605
  • 3
  • 34
  • 52
  • 1
    Yep, the edited answer looks like the working solution. `app.post` should be used only if you expect to get your app requested via POST. If you accept GET requests then `app.get` will work better. – Krasimir Nov 20 '13 at 07:00
  • 1
    Yep, you should change `app.get` to `app.post`, but you should also change the way of how you get the request parameters. I'll suggest to read this thread http://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js – Krasimir Nov 20 '13 at 07:24