我正在尝试async.parallel
从单个 URL 中的两个不同表中获取 JSON 响应的功能
- 我在调试错误时遇到问题
场景是这样的
有一个数据库
该数据库中有两个表
我正在使用单个请求异步转换两个表的 JSON 响应
有任何想法吗
我的快递程序::
var express = require('express')
, async = require('async')
, http = require('http')
, mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'xxxx',
password: "xxxx",
database: 'test123'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 8084);
app.get('/',function(request,response){
var first, second;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM test1', function(err, rows, fields)
{
console.log('Connection result error '+err);
first = JSON.stringify(rows);
callback();
});
},
// Get the second table contents
function ( callback ) {
connection.query('SELECT * FROM test2', function(err, rows, fields)
{
console.log('Connection result error '+err);
second = JSON.stringify(rows);
callback();
});
}
// Send the response
], function ( error, results ) {
response.writeHead(200, { 'Content-Type': 'application/json'});
response.end({
'first' : first,
'second' : second
});
} );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
错误::
Express server listening on port 8084
Connection result error null
Connection result error null
TypeError: first argument must be a string or Buffer
at ServerResponse.OutgoingMessage.write (http.js:783:11)
at ServerResponse.OutgoingMessage.end (http.js:964:16)
at /home/ubuntu/rainmelon/sample_programs/express_folder_json/app.js:55:18
at /home/ubuntu/rainmelon/sample_programs/express_folder_json/node_modules/async/lib/async.js:229:13
at /home/ubuntu/rainmelon/sample_programs/express_folder_json/node_modules/async/lib/async.js:139:25
at /home/ubuntu/rainmelon/sample_programs/express_folder_json/node_modules/async/lib/async.js:226:17
at /home/ubuntu/rainmelon/sample_programs/express_folder_json/node_modules/async/lib/async.js:553:34
at Query._callback (/home/ubuntu/rainmelon/sample_programs/express_folder_json/app.js:48:21)
at Query.Sequence.end (/home/ubuntu/rainmelon/sample_programs/node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24)
at Query._handleFinalResultPacket (/home/ubuntu/rainmelon/sample_programs/node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
编辑后
快递程序::
var express = require('express')
, async = require('async')
, http = require('http')
, mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'xxx',
password: "xxx",
database: 'test123'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 8084);
app.get('/',function(request,response){
var first, second;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM test1', function(err, rows, fields)
{
console.log('Connection result error '+err);
first = JSON.stringify(rows);
callback();
});
},
// Get the second table contents
function ( callback ) {
connection.query('SELECT * FROM test2', function(err, rows, fields)
{
console.log('Connection result error '+err);
second = JSON.stringify(rows);
callback();
});
}
// Send the response
], function ( error, results ) {
response.json({
'first' : first,
'second' : second
});
} );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});