How to print object in Node JS

前端 未结 6 2043
忘掉有多难
忘掉有多难 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: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
      }
    });
    

提交回复
热议问题