Node.js Express生成JSON
我试图传递一个键值,并根据传递的键值生成JSON响应 快速节目Node.js Express生成JSON,node.js,express,Node.js,Express,我试图传递一个键值,并根据传递的键值生成JSON响应 快速节目 var express = require('express'), async = require('async'), http = require('http'), mysql = require('mysql'); var app = express(); var connection = mysql.createConnection({ host: 'localhost', user
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'));
});
测试运行::
我得到了RestaurantMings的JSON响应,但不是第一个传递keyvalue的响应,如何解决这个问题?
您想要的路径是
/
,而不是/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"
}
]
}
尝试参考以下链接可能会有所帮助
路径有两个处理程序
/RestaurantDesc/
-只应保留一个
在您的查询中,语法是错误的
假设您有一个如下所示的表(更改这些名称以匹配您的架构):
如果您正在使用,请将查询更改为以下内容:
connection.query('SELECT * FROM Restaurant where name = ?', [keyName], function (err, rows, fields) {
console.log('Connection result error ' + err);
name_of_restaurants = rows;
callback();
});
那个?是在查询的第二个参数中传递的值数组中传递的值的占位符
请参阅:以了解有关其工作原理的更多信息。简短的回答是,使用这种方法将正确转义值。我指的是app.get(“/RestaurantDesc/”…请检查我发布的pitchire中的测试场景….将keyname传递到query是我的所在,在“/”中它对您有效,不?只使用其中的任何内容。或者我可能不了解您的问题?不在“/”。在“/”中,它按照您所说的那样工作…我指的是“/RestaurantDesc/”,在这里我将keyvalue传递给sql呃……我是这个程序的新手。看起来这里至少有两个问题:(1)就像@sza之前说的,你有2个/RestaurantDesc/
处理程序。只保留一个。(2)您的SQL查询是错误的。看起来您是从通过键传递的表中进行选择的,实际上您应该在where
子句(餐厅名称=
)中包含它。最后,您应该注意将输入转义到SQL。感谢此i/p…您能用我的代码(w.r.t)编辑您的答案吗你的解决方案…它会很有用!任何处理sql查询的express链接?postgresql by mysql假设你正在使用,请看一看:这很有帮助!…但是如果你在我的代码中冷编辑查询部分作为答案…这种实际的理解将对我有很大帮助…我是这方面的新手!谢谢!…这是非常有用的这正是我想要的!……我还需要重新思考我的数据库设计……起初我错了!……我从你的回答中学到了很多
connection.query('SELECT * FROM Restaurant where name = ?', [keyName], function (err, rows, fields) {
console.log('Connection result error ' + err);
name_of_restaurants = rows;
callback();
});