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

2 Answers 2

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
            }
        })
});
Sign up to request clarification or add additional context in comments.

4 Comments

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"}]]
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
and can we use some data from result1 in second function?
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)
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);

1 Comment

Appreciate your efforts but the reference links you provided was confusing and complicated. But thanks though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.