我想绑定(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/