我正在尝试传递一个键值并根据传递的键生成 JSON 响应
快递计划
var express = require('express'),
async = require('async'),
http = require('http'),
mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: "root",
database: 'restaurants'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 7004);
app.get('/RestaurantDesc/', function (request, response, next) {
var keyName = request.query.Key;
var name_of_restaurants, RestaurantTimings;
async.series([
// Get the first table contents
function (callback) {
connection.query('SELECT * FROM ', keyName, function (err, rows, fields) {
console.log('Connection result error ' + err);
name_of_restaurants = rows;
callback();
});
},
// Get the second table contents
function (callback) {
connection.query('SELECT * FROM RestaurantTimings', function (err, rows, fields)
{
console.log('Connection result error ' + err);
RestaurantTimings = rows;
callback();
});
}
// Send the response
], function (error, results) {
response.json({
'restaurants': name_of_restaurants,
'RestaurantTimings': RestaurantTimings
});
});
});
app.get('/RestaurantDesc/', function (request, response, next) {
var keyName = request.query.Key;
var name_of_restaurants, RestaurantTimings;
async.series([
// Get the first table contents
function (callback) {
connection.query('SELECT * FROM ', keyName, function (err, rows, fields) {
console.log('Connection result error ' + err);
name_of_restaurants = rows;
callback();
});
},
// Get the second table contents
function (callback) {
connection.query('SELECT * FROM RestaurantTimings', function (err, rows, fields)
{
console.log('Connection result error ' + err);
RestaurantTimings = rows;
callback();
});
}
// Send the response
], function (error, results) {
response.json({
'restaurants': name_of_restaurants,
'RestaurantTimings': RestaurantTimings
});
});
});
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
测试运行::
我得到的是 RestaurantTimings 的 JSON 响应,但不是第一个传递 keyvalue 的响应,如何解决这个问题?