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: '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

0

您可以更改以下功能:

function ( error, results ) {
    response.json({
        'first' : first,
        'second' : second
    });
}

function ( error, results ) {
    // case 1: since results is a js array object you can do following and is recommended
    res.json({first: results[0], second: results[0]});
    // case 2:else you can do the following thing as well. (Not recommended in this case of async)
    /* var jsonObj = JSON.parse('{first :' + first +', second : ' + second + '}');
    response.json(jsonObj); */
}

以下将是输出:

{
  first :[{"_id":1,"name":"devrath","age":24},{"_id":2,"name":"Dheeraj","age":25},{"_id":3,"name":"Vikas","age":26}], 
  second : [{"_id":1,"place":"Hassan","phone":1},{"_id":2,"place":"Pune","phone":2},{"_id":3,"place":"Delhi","phone":3}]
}

在您的函数中,变量实际上是字符串类型(不是对象),因此您需要在发送响应之前将其解析为 JSON(如 case2 所示)。但是实际上您忘记了results包含所有结果的对象本身,所以最好按照使用for和forasync.series functions中提到的那样进行处理(在这种情况下,您可以像以前一样忽略存储在变量本身中)case 1results[0]firstresults[1]secondfirst = JSON.stringify(rows);

为了您的信息,您可以考虑Content-Type在响应之前进行设置。更多检查快递文档

于 2013-08-21T05:54:41.110 回答
0

您不需要stringifyMySql 驱动程序的结果。此外,您不需要将结果存储到变量中,因为这正是async.js对您有用的。

因此,代码应如下所示:

var tasks = ['test1', 'test2'].map(function (table) {
  return function (callback) {
     connection.query('SELECT * FROM ' + table, function (err, rows) {
         if (err) {
           return callback(err);
         }

         callback(null, rows);
     });
  }
});

async.series(tasks, function (err, results) {
   if (err) {
     return res.send(500);
   }

   res.json({first: results[0], second: results[1]});
});
于 2013-08-21T08:13:30.970 回答