Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 节点js+异步返回结果连接数据_Jquery_Node.js_Postgresql - Fatal编程技术网

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

Jquery 节点js+异步返回结果连接数据,jquery,node.js,postgresql,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.row

我想绑定异步的数据并通过回调方法发送,这是在另一个函数中定义的,但是我得到了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' }]

client.query从通讯详细信息中选择名字、姓氏、政党id、城市、州、国家/地区,如“ram%”、函数错误、结果{ async.eachresult.rows,functionvalue,回调{ var party\u id=value.party\u 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);
});

};

您的内部查询中存在无效的SQL,即未指定表名的起始位置部分。但这不是主要问题。您正在中断异步查询和管理连接会话之间的顺序,即在错误的时间调用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替换为缺少的表名。此外,使用此库,您不需要释放连接,它会在序列完成时自动完成

所有逻辑都是通过async.each获得的

这样更干净、更安全、更可读。欢迎来到承诺的世界


顺便说一句,您正在做的事情的最终结果也可以通过一个连接查询来实现。

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

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

您的回调在client.queryselect*from..,functionerr scope外部一旦我将回调放在内部,我就会收到错误消息,在发送后无法设置头。可能已完成;应该在client.queryselect*from..,functionerr?之前。我不知道您使用的是哪一个数据库客户端我正在使用postgresql,如果我保持不变的话他在client.query之前完成了,我得到了相同的错误,在发送头之后无法设置头您使用async.eachresult.rows,functionvalue,done{要使用父级`client.query`result进行操作,但不要使用子级`client.query`的result1。也许您也应该将其包装?。将value1'和done1作为result1?。如果我将其放在client.query中,则会在发送后抛出一个错误“发送后无法设置头”错误n为一个请求发送多个响应。这可能是您的“完成”函数“发送回调”,您再次发送带有result1的回调。删除“完成”后,我也面临同样的问题,我的查询将给出我要在单个响应中发送结果的多列的结果。//async中的第一个参数。每个参数是项的数组async.eachitems,//第二个参数是将每个项传递给functionitem的函数,回调{//调用异步函数,从DB item中选择字段。someAsyncCallfunction{//异步调用完成,通过回调发出警报;};},//第三个参数是在所有操作完成时调用的函数{//所有任务现在都已完成doSomethingOnceAllAreDone;}我们将从父查询中得到结果,并使用for循环将其传递给子查询,但是循环不会一直到结束,它将在中间停止,并返回结果中间的未完成的结果。一旦循环完成,您将得到完整的结果:可以访问T的cDebug。他完成了顶级查询结果,并且在参数数据中,您将拥有完整的计数器列表。因此,使用回调的地方将拥有它需要的所有数据。这取决于您如何将其格式化返回,但它都在那里。我不明白为什么您要说循环将在中间停止-它不会;调用PR。omise.all保证循环的完成。此代码仍然无效,因为它调用的位置可能仍然在执行异步查询。使用回调也是如此,它位于异步之外,这意味着它不是真正的异步?看起来非常错误。