2

I was doing some basic JSON parsing and wondered why node js collapses the objects when it is logged together with string

so for example in the code below, if I go console.log(processedData) it won't collapse the object and show the whole string but if I go console.log('Body: ' + processedData) it collapses the objects and goes [object Object][object Object].... I know how to expand them again using util but I was curious on the logic behind it as I am quite new to node. I think I might be missing out on something.

const http = require('http');
const util = require('util');

var options = {
host: 'http://nodejs.org/dist/index.json'
// host: 'en.wikipedia.org',
// path: '/w/api.php?action=query&list=allpages&apfrom=Imperial&aplimit=500&format=json'
};

var req = http.get('http://nodejs.org/dist/index.json', function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));

let body = '';
res.on('data', function(chunk) {
    body += chunk;
}).on('end', function() {
    let processedData = JSON.parse(body);
    console.log('Body : ' + processedData);
    console.log(typeof body);
})
});

req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});

3 Answers 3

4

When you call

console.log(processedData)

You're passing the entire object to console.log for it to do it's thing. However, when you call

console.log('Body: ' + processedData)

You're passing the result of 'Body: ' + processedData to console.log. The evaluation of this expression causes processedData to be converted to its string representation, which if you haven't defined toString on your object, will just be [object Object], so you see Body: [object Object] being logged.

The simplest way to achieve what you're after is to simply pass them as separate arguments:

console.log('Body: ', processedData)
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe we should answer following questions: "If you do the operation like below - what is expected a type of result?"

var myVar = "Body : " +  processData;

When js engine tries to evaluate such expression it knows that first parameter of the expression is 'string', so it tries to concatenate the string with another string. How processData become a string? By calling 'toString()' on processData.

To achieve the result you expect, to try to use console.log in that way:

console.log("Body:", processData);

Comments

0

This is because JS type coercion.

In first case you print it as object, but in second, you add it to the string ('Body: ' + processedData) - and according to JS type coercion rules it converts it to string (it concatenates it to string) You can use util module as you suggested, or to use console.dir({body:processedData},{colors:true,depth:4});

Comments

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.