节点js +异步返回结果连接数据

时间:2015-08-19 08:22:07

标签: jquery node.js postgresql

我想绑定异步数据并通过回调方法发送,该方法在另一个函数中定义,但我得到的result1错误未定义任何人都可以帮助我。

    client.query("select first_name,last_name,party_id,city,state,country from communication_detail where lower(first_name) like 'ram%'", function(err, result) {
        async.each(result.rows, function(value, done) {
            var party_id = value.party_id;

            client.query("select count(*) as goal_count from where creater_id = '"+party_id+"'", function(err, result1) {
                value.push(result1.rows[0].goal_count);
            });
            done();
        });

        callback(err, result1);

    });

实际结果得到:

[{ first_name: 'sai', last_name: 'kishore', party_id: 58, city: null, state: null, country: null},{ first_name: 'Saikishore', last_name: 'P', party_id: 50, city: null, state: null, country: 'India' }, { first_name: 'kishore', last_name: 'sai', party_id: 57, city: 'Telangana', state: 'Telangana', country: 'India,India' }]

预期结果:

[{ first_name: 'sai', last_name: 'kishore', party_id: 58, city: null, state: null, country: null, goal_count: 10 },{ first_name: 'Saikishore', last_name: 'P', party_id: 50, city: null, state: null, country: 'India', goal_count: 0 }, { first_name: 'kishore', last_name: 'sai', party_id: 57, city: 'Telangana', state: 'Telangana', country: 'India,India', goal_count: '252' }]

3 个答案:

答案 0 :(得分:0)

您的内部查询中存在无效的SQL,printf("distance between %p & %p: %td\n", (void *) a0, (void *) a1, difference); 部分未指定表名。但这不是主要问题。您打破了异步查询和管理连接会话之间的顺序,即在错误的时间调用from where

我不能建议你修复它,因为你试图实现的目的是完全依赖回调是非常尴尬的。

您需要简化运行所有异步查询的逻辑。

我可以提供的最佳解决方案,使用pg-promise实现。

您拥有的逻辑示例如下所示:

done

请注意,我将db.query("select first_name, last_name, party_id, city,state, country from communication_detail where lower(first_name) like 'ram%'") .then(function (cDetails) { var requests = []; for (var i = 0; i < cDetails.length; i++) { var q = db.query("select count(*) as goal_count from xxx where creater_id = $1", cDetails[i].party_id); reguests.push(q); } return promise.all(requests); }) .then(function (data) { for (var k = 0; k < data.length; k++) { // data[k].goal_count - your counter values; // append them where you want; } callback(/* value, or whatever else you need to pass...*/); }); 替换为缺少的表名。此外,使用此库,您无需释放连接,它在序列完成时自动完成。

xxx逻辑正是您通过promise.all获得的。

这种方式更加清洁,更安全,更易读。欢迎来到承诺的世界! ;)

顺便说一句,您所做的最终结果也可以通过一个async.each查询来实现。

答案 1 :(得分:0)

在后端代码中推送方法不起作用。它可能无法给出预期的结果。请尝试以下代码。希望它会对你有所帮助。

client.query("select first_name,last_name,party_id,city,state,country from communication_detail where lower(first_name) like 'ram%'", function(err, result) {
async.each(result.rows, function(value, done) {
    var party_id = value.party_id;

    client.query("select count(*) as goal_count from table_name where creater_id = '" + party_id + "'", function(err, result1) {
        value.goal_count = 0;

        if (result1.rows.length > 0) {
            value.goal_count = result1.rows[0].goal_count;

        }
    });
    done();
});

callback(err, result1);}); 

答案 2 :(得分:-1)

client.query(“从communication_detail选择first_name,last_name,party_id,city,state,country,其中lower(first_name)喜欢'ram%'”,function(err,result){     async.each(result.rows,function(value,callback){         var party_id = value.party_id;

    client.query("select count(*) as goal_count from where creater_id = '"+party_id+"'", function(err, result1) {
        // value.push(result1.rows[0].goal_count);
        value.goal_count = result1.rows[0].goal_count;
        callback();
    });    
}, function(err) {
  callback(err, result);
});

});