Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to this node.js and postgres.. I am executing 2 queries(select) and i have to combie these 2 queries output into a single result. Also i have to make the 1st result to appear first and then the 2nd query.. I have the following code but i am not achieving the requirement. Also i am not sure that the code i have is correct..Suggest other idea if you have..

My code:

  client.query("select * from tn_village where level3 ILIKE '%"+villagename+"%' OR level4 ILIKE '%"+villagename+"%' OR level5 ILIKE '%"+villagename+"%' OR level6 ILIKE '%"+villagename+"%' OR level7 ILIKE '%"+villagename+"%' OR level8 ILIKE '%"+villagename+"%' OR level9 ILIKE '%"+villagename+"%'",function(err,result)
    {
    client.query("select * from tn_village where level3 ILIKE '%"+village+"%' OR level4 ILIKE '%"+village+"%' OR level5 ILIKE '%"+village+"%' OR level6 ILIKE '%"+village+"%' OR level7 ILIKE '%"+village+"%' OR level8 ILIKE '%"+village+"%' OR level9 ILIKE '%"+village+"%'" ,function(err,result1)
    {      
    res.send(result1);
    });
    });

How can i combine result and result1 to a single output..Help me to solve this..Thanks in advance..

share|improve this question
add comment

1 Answer

Well, simply following the way you have it laid out (although I prefer promises even in node for aggregating multiple async calls):

 client.query("select ...",function(err,result) {
    client.query("select ..." ,function(err,result1) {  
        var ret = {
            result: result,
            result1: result1
        }
        res.send(ret);
    });
 });

That is the simplest example I can think of to show you how easy this should be. Now, if for example you are thinking of some sort of loop over result and result1 that will combine/munge them together etc, or if you want to concatenate the two arrays into one etc, all of that isn't clear by your question but is easy to do.

Let me know if that clarifies at all. In essence the value of "result" is still available to you in the inner callback, so you can make use of it in your res.send.

PS - nesting calls that don't necessarily need to be nested is a bit derpy, promises or other callback aggregators would help here.

share|improve this answer
 
Thanks @Nick Sharp..It's returning only 1st query result only.. –  Subburaj 10 mins ago
 
r u there...... –  Subburaj 5 mins ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.