如何在express中创建一个insert语句

时间:2013-11-20 06:26:11

标签: node.js express

我在

下面有快递服务器代码
  • 我想通过获取param
  • 来启动插入语句
  • 然后inserting param值进入数据库

到目前为止我所做的是我已经学会了如何制作JSON响应::

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'));
});

我想做什么::

  • 我想找到如何制作插入声明
  • 这样我就可以提取Param值并将数据插入 数据库
  • 如何修改上述代码以实现我的目标

希望我很清楚!


[编辑]

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'));
});

1 个答案:

答案 0 :(得分:2)

我不熟悉您使用的库,但我认为您应该在传递给async.series的数组中添加另一个函数。函数体应具有与上面相似的内容。

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();
      }
    );
  }
]

将会发生两个函数异步执行,最后您仍然会向浏览器发送响应。

您已经通过var keyName=request.query.Key;获得了GET参数。

如果您打算使用POST参数,而不是

var keyName=request.query.Key;

您应该添加一个解析变量的中间件:

app.use(express.bodyParser());

然后

var keyName = request.body.Key;

我引用此评论https://stackoverflow.com/a/18167056/642670