How to print object in Node JS

前端 未结 6 2021
忘掉有多难
忘掉有多难 2020-12-18 22:49

In the below code (running on Node JS) I am trying to print an object obtained from an external API using JSON.stringify which results in an error:

相关标签:
6条回答
  • 2020-12-18 23:20

    You can pass two arguments to console.log()

    Try this code after installing "yargs" And it will print whole object

    console.log('object is' , yargs.argv);

    I think may be it will help you to print whole object :)

    0 讨论(0)
  • 2020-12-18 23:33

    Basic console.log will not go through long and complex object, and may decide to just print [Object] instead.

    A good way to prevent that in node.js is to use util.inspect:

    'use strict';
    const util = require('util'),
        obj = /*Long and complex object*/;
    
    console.log(util.inspect(obj, {depth: null}));
    //depth: null tell util.inspect to open everything until it get to a circular reference, the result can be quite long however.
    

    EDIT: In a pinch (in the REPL for example), a second option is JSON.stringify. No need to require it, but it will break on circular reference instead of printing the fact there is a reference.

    0 讨论(0)
  • 2020-12-18 23:38

    By using the http request client, I am able to print the JSON object as well as print the country value. Below is my updated code.

    var request = require('request');
    request('http://ip-api.com/json', function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(response.body);    // Prints the JSON object
        var object = JSON.parse(body);
        console.log(object['country']) // Prints the country value from the JSON object
      }
    });
    
    0 讨论(0)
  • 2020-12-18 23:41

    This can print the key of the object and the value of the object in the simplest way. Just try it.

    const jsonObj = {
      a: 'somestring',
      b: 42,
      c: false
    };
    
    Array.from(Object.keys(jsonObj)).forEach(function(key){
      console.log(key + ":" + jsonObj[key]);
    });
    
    0 讨论(0)
  • 2020-12-18 23:44

    Print the whole object, it will not have problems with recursive refferences:

    console.log(res);
    

    Here's an example for you to see how console.log handles circular refferences:

    > var q = {a:0, b:0}
    > q.b = q
    > console.log(q)
    { a: 0, b: [Circular] }
    

    Also, I would advise to check what data are you actually receiving.

    0 讨论(0)
  • 2020-12-18 23:44

    You do not actually get data in res. You need on('data') and on.('end')

    body is a string. It gets append on data received, so on complete you will need to parse data into json

    http.get("http://ip-api.com/json", function(res) {
        var body = '';
        res.on('data', function(data){
            body = body + data;
        });
    
        res.on('end', function() {
            var parsed = {};  
            try{
                parsed = JSON.parse(body); // i have checked its working correctly
            }
            catch(er){
                //do nothing it is already json
            }
            console.log(parsed.country);
        });
    });
    

    Noe from parsed which is a json object, you can get any property

    0 讨论(0)
提交回复
热议问题