0

我正在尝试传递一个键值并根据传递的键生成 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 的响应,如何解决这个问题?

在此处输入图像描述

4

3 回答 3

1

尝试参考以下链接可能会有所帮助

http://book.mixu.net/ch10.html

http://expressjs.com/api.html

于 2013-08-30T05:11:03.940 回答
0

做你想做的事的路径是/,不是/RestaurantDesc/。看第一个参数app.get()

这就是我得到的http://54.218.73.244:7004/

{
  "restaurants": [
    {
      "restaurantID": 1,
      "restaurantNAME": "CopperChimney",
      "url": "http://54.218.73.244:7002/CopperChimney"
    },
    {
      "restaurantID": 2,
      "restaurantNAME": "Aroy",
      "url": "http://54.218.73.244:7002/Aroy"
    },
    {
      "restaurantID": 3,
      "restaurantNAME": "MarkBoulevard",
      "url": "http://54.218.73.244:7002/MarkBoulevard"
    },
    {
      "restaurantID": 4,
      "restaurantNAME": "Indian",
      "url": "http://54.218.73.244:7002/Indian"
    }
  ],
  "RestaurantTimings": [
    {
      "_id": 1,
      "RestaurantTime": "8pm to 11pm"
    },
    {
      "_id": 2,
      "RestaurantTime": "10pm to 12pm"
    },
    {
      "_id": 3,
      "RestaurantTime": "11pm to 9pm"
    },
    {
      "_id": 4,
      "RestaurantTime": "10pm to 5pm"
    }
  ]
}
于 2013-08-30T04:57:54.233 回答
0

您有两个用于路径的处理程序/RestaurantDesc/- 应该只保留一个。

在您的查询中,语法错误。

假设您有一个如下所示的表(更改这些名称以匹配您的架构):

Table Name: Restaurant
    column:       name

而您正在使用node-mysql,将您的查询更改为以下内容:

connection.query('SELECT * FROM Restaurant where name = ?', [keyName], function (err, rows, fields) {
    console.log('Connection result error ' + err);
    name_of_restaurants = rows;
    callback();
});

这 ?是在第二个参数中传递给的值数组中传递的值的占位符query

有关其工作原理的更多信息,请参阅:转义查询值。简短的回答是,使用这种方法可以正确转义这些值。

于 2013-08-30T05:57:51.043 回答