user2914530
user2914530

Reputation: 1

How to print the JSON output in nodejs

I want to get an output for the code

app.get('/_browse', function(req, res){

    var filter = {};
    var type = req.query["type"] || '';
    var nodeValue =  (req.query["nodeValue"]) ? req.query["nodeValue"] : "";
    var nodeApi =  (req.query["nodeApi"]) ? req.query["nodeApi"] : "";
    var node =  (req.query["node"]) ? req.query["node"] : "";
    var callback = req.query["callback"] || '';
    /*var mapobject = {
        kingdom : "phylum",
        phylum : "class",
        class : "order",
        order : "family",
        family : "genus",
        genus : "specificEpithet",

    };*/
    var text = {
            text: nodeValue,
            specimenCount: 'count',
            nodeApi: nodeApi,
            nodeValue: nodeValue,
                }

    var mapobject = {

        phylum : "family",
        family : "genus",
        genus : "specificEpithet",

    };
    var type = mapobject[nodeApi];  
    var nvalue = {};
    if(nodeValue != ''&& nodeApi != '') {
     nvalue[nodeApi] = nodeValue;
    }
    var typeValue = "$"+ type;

collection.aggregate(

        { $match : nvalue},
        { $group : {
            _id : typeValue,
            count : { $sum : 1 },
        }},
        {$sort: {_id: 1}},
    function(err, results) {
        /*results[0]['text'] = text;
        console.log("text values",results[0]['text']);*/
            /*var text = {
            text: "Aceraceae ",
            specimenCount: 'count',
            nodeApi: "Family",
            nodeValue: "Aceraceae",
                }
            results['text'] = text;
            console.log("text values",results.text);
            */  
        (var i in results){
            if(results = text){
                results.text =  nodeValue;
                results.specimenCount = 'count';
                results.nodeApi = nodeApi;
                results.nodeValue = nodeValue;
            console.log("results",results.text);
            }
        res.writeHead(200, {'Content-Type': 'application/json'});
        if (callback != '') {
            res.end(callback + '(' + JSON.stringify({success: true, rows: results}) + ')');

        } else {
                res.end(JSON.stringify({success: true, rows: results}));


            }

    });

}); 

like...

    results: [{
       text: "Acanthaceae (300)",
       specimenCount: "300",
       nodeApi: "Family",
       nodeValue: "Acanthaceae",
       filter: {
           StateProvince: "Louisiana",
           Taxonomy: "",
           Phylum: "dicot"
       }
   },

I did my coding like this way but it is not giving the output. It need to process the results to make this output. Please help me to get the output.

Upvotes: 0

Views: 241

Answers (1)

user568109
user568109

Reputation: 48003

If you want to print results like above you can do :

  1. console.log("Result: %j", results);
  2. console.log(require('util').inspect(results, false, null));

Upvotes: 1

Related Questions