Last callback not being called using async

余生颓废 提交于 2019-12-11 02:36:51

问题


I cannot seem to get the last callback (commented as "optional callback") called to send the result back to the browser. Any pointers as to what I am doing wrong? I am using the following modules: async, restify and postgresql for node.js

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);
        })
    });
}

回答1:


I'm not sure why you're using async since you're only calling one asynchronous function. But the reason your callback isn't called is because you're not ending the first function by calling its callback:

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);
});

Or, shorter:

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

Or, in this case, even shorter (and without the need for async):

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



回答2:


Saw my mistake, missing the line where I should have returned the callback as in:

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


来源:https://stackoverflow.com/questions/16375983/last-callback-not-being-called-using-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!