如何在nodejs中打印JSON输出

时间:2013-10-30 05:01:09

标签: node.js mongodb

我想获得代码的输出

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


            }

    });

}); 

像...

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

我按照这种方式编写了代码,但它没有提供输出。 它需要处理结果以进行此输出。 请帮我拿出输出。

1 个答案:

答案 0 :(得分:1)

如果你想像上面那样打印results,你可以这样做:

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