最近没有使用异步调用回调

时间:2013-05-04 15:35:30

标签: javascript node.js asynchronous restify

我似乎无法将最后一个回调(注释为“可选回调”)调用以将结果发送回浏览器。关于我做错了什么的指示?我使用以下模块:node.js的async,restify和postgresql

console.log('Start');

var async = require('async');
var restify = require('restify');
var server = restify.createServer();
server.use(restify.bodyParser());

server.get('/user/creationdate/:username', function(req, res, next) {
    var userName = req.params.username;
    var record;

    async.parallel([
        function(callback){
            getUserByName(userName, function(err, user) {
                if (err) return callback(err);
                record = user;
            });
        }
    ],
// optional callback
        function(err){
            console.log('5. Following record has been retrieved:' + record);
            res.send(record);
        });

    next();
});

server.listen(8080, function () {
    console.log('%s listening at %s', server.name, server.url);
});

handleError = function handleError(err) {
    if(!err) { return false; }
    else {
        console.log('The following error occurred:' + err);

    }
    return true;
};

function getPgClient(){
    var pg = require('pg');

    var client = new pg.Client({
        user: 'postgres',
        password: 'password',
        database: 'foobar',
        host: '192.168.1.100',
        port: 5432
    });

    client.on('drain', client.end.bind(client)); //disconnect client when all queries are finished

    return client;
}

function getUserByName(userName, callback){
    var client = getPgClient();
    console.log('2. Trying to connect to DB');

    client.connect(function(err) {
        console.log('3. Error connecting to DB:' + handleError(err));
        if(handleError(err)) return callback(err);

        client.query("SELECT created_at FROM users WHERE username='" + userName + "'", function(err, result) {
            if(handleError(err)) return;
            console.log('4. Error occurred:' + err);
            console.log(result);
            console.log(callback);
            callback(null, result);
        })
    });
}

2 个答案:

答案 0 :(得分:4)

我不确定你为什么使用async,因为你只调用一个异步函数。但是没有调用回调的原因是因为你没有通过调用它的回调来结束第一个函数:

async.parallel([
  function(callback) {
    getUserByName(userName, function(err, user) {
      // call async callback with err and user
      callback(err, user);
    });
  }
], function(err, record) {
  console.log('5. Following record has been retrieved:' + record);
  res.send(record);
});

或者,更短:

async.parallel([
  function(callback) {
    getUserByName(callback);
  }
], function(err, record) {
  console.log('5. Following record has been retrieved:' + record);
  res.send(record);
});

或者,在这种情况下,甚至更短(并且不需要async):

getUserByName(function(err, record) {
  // handle error, or ...
  console.log('5. Following record has been retrieved:' + record);
  res.send(record);
});

答案 1 :(得分:0)

看到我的错误,错过了应该返回回调的行,如下所示:

    async.parallel([
    function(callback){
        getUserByName(userName, function(err, user) {
            if (err) return callback(err);
            record = user;
            return callback(null, record);
        });
    }