0

Following is my code of in node.js i would like to know how to append two results of query in one json and send the json in response.

        router.get("/get-meta", function(req, res, next){

    connection.query("select id, country_name from country", function (error, results) {

        if (error) res.json({"status": "failed", "message": error.message});
        else res.send(JSON.stringify({ results }));
    });
    connection.query("select id, currency from currency", function (error, results2) {

        if (error) res.json({"status": "failed", "message": error.message});
        else res.send(JSON.stringify({ results2 }));
    });
});
Azeem Ullah
  • 125
  • 1
  • 3
  • 12

2 Answers2

3

use async.parallel to send parallel requests.the result of async.parallel will be an array of results (which will solve your problem)

var async = require('async')

router.get("/get-meta", function(req, res, next){

    async.parallel([
        function(callback){
            connection.query("select id, country_name from country", function (error, result1) {
                callback(error,result1)
            });
        },
        function(callback){
            connection.query("select id, currency from currency", function (error, result2) {
                callback(error,result2)
            });
        }

        ],function(err,results){
            if(err){
                res.json({"status": "failed", "message": error.message})
            }else{
                res.send(JSON.stringify(results)); //both result1 and result2 will be in results
            }
        })
});
dev07
  • 346
  • 2
  • 8
  • How can I give heading to each result-set while returning response? For example i am getting data like: [[{"id":1,"country_name":"Pakistan"}],[{"id":1,"currency":"PKR"}]] to [country: [{"id":1,"country_name":"Pakistan"}], currency:[{"id":1,"currency":"PKR"}]] – Azeem Ullah May 27 '17 at 11:05
  • you cannot specify headers ,but the results will be in order.so you can asume the results of query1( first function) will be in `results[0]` and query2 will be in `results[1]` and so on – dev07 May 27 '17 at 11:09
  • and can we use some data from result1 in second function? – Azeem Ullah May 27 '17 at 11:21
  • it is parallel function both functions will run together so you cannot use data from one func. in another,but you want such operation you can use `async.waterfall` (but this will run your functions in series) – dev07 May 27 '17 at 11:26
1

First of all, you should find a method to await for two async operation to finished, and then concat the results. Have a look here or here

You should use concat method.

Here is an example:

var json1 = [{'name': "doug", 'id':5}];
var json2 = [{'name': "goud", 'id':1}];
json1 = json1.concat(json2);
console.log(json1);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • 1
    Appreciate your efforts but the reference links you provided was confusing and complicated. But thanks though. – Azeem Ullah May 27 '17 at 11:14