jquery - Node js + 异步返回结果加入数据

标签 jquery node.js postgresql

我想绑定(bind)异步数据并通过回调方法发送,该方法在另一个函数中定义,但我收到 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' }]

最佳答案

在后端代码中 push 方法将不起作用。它可能不会给出预期的结果。试试下面的代码。希望对您有所帮助。

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

关于jquery - Node js + 异步返回结果加入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32090167/

相关文章:

javascript - 当你 .extend() 一个 obj 原型(prototype)自身时会得到什么

jquery - 仅悬停在图像上

postgresql - 在 PostgreSQL 中结合 CTE 和 IN

php - 使用 $_POST 参数创建表

postgresql - 名为 "postgres"的数据库

jquery - 剖析 img src 属性

javascript - 尝试在导航中使用 jQuery 的 .addClass 方法

javascript - 这个任务队列可以使用哪个库?

javascript - 自由函数调用中的 this.property 在我的浏览器控制台和 NodeJS REPL 中有效,但在作为 NodeJS 脚本运行时无效。为什么?

mysql - PROTOCOL_SEQUENCE_TIMEOUT