表达生成JSON [英] Express to generate JSON
问题描述
我尝试传递一个关键值,并根据传递的密钥生成JSON响应
Express Program
Express Program
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的第一个响应,如何解决这个问题?
推荐答案
您有两个处理程序路径 / RestaurantDesc /
- 只有一个应该保留。
You have two handlers for the path /RestaurantDesc/
- only one should be kept.
在您的查询中,语法错误。
In your query, the syntax is wrong.
假设您有一个看起来像这样的表(更改这些名称以匹配您的模式):
Assuming you have a table that looks like this (change these names to match your schema):
Table Name: Restaurant
column: name
而您正在使用 node-mysql ,将查询更改为这个:
And you are using node-mysql, Change your query to something like this:
connection.query('SELECT * FROM Restaurant where name = ?', [keyName], function (err, rows, fields) {
console.log('Connection result error ' + err);
name_of_restaurants = rows;
callback();
});
是在第二个参数中传递给查询
的值的数组中传递的值的占位符。
The ? is a place holder for values passed in the array of values passed in the second parameter to query
.
请参阅:转义查询值以获取有关如何工作的更多信息。简单的答案是,使用这种方法可以正确地转义这些值。
See: Escaping query values for more information on how this works. Short answer though is that the values will be escaped properly using this approach.
这篇关于表达生成JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!