0

我正在尝试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'));
});
4

2 回答 2

1

错误与async.parallel(). 堆栈跟踪提到:

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

大概55是:app.js

response.end({

问题是内置response.end()不支持自动序列化Object到 JSON。它只接受 aString或 a Bufferfor data

你要么需要stringify自己Object

response.end(JSON.stringify({
    // ...
}));

或者,您可以使用 Express'.send().json().

// Send the response
], function ( error, results ) {
    response.send({
        'first' : first,
        'second' : second
    });
} );

后者中的任何一个也Content-Type将为您设置。

于 2013-08-21T03:14:17.780 回答
0

我认为,如果您仔细查看 async.js 文档中的示例,您会注意到保存每个结果的“第一个”和“第二个”变量的范围是错误的。您应该使用它们各自的错误和响应调用每个回调,然后使用示例中的数组来构建要提交的最终对象。(或者您可以使用 globar var,但这不是一个好主意)。干杯。

async.series([
    function(callback){
        // do some stuff ...
        callback(null, 'one');
    },
    function(callback){
        // do some more stuff ...
        callback(null, 'two');
    }
],
// optional callback
function(err, results){
    // results is now equal to ['one', 'two']
});
于 2013-08-21T03:50:11.997 回答