go back

Unveiling Nested Objects: A Guide to Enhanced Console Logging in Node.js

Oct 24, 2023

Sometimes when working in Node environments, console logging objects doesn't provide the information you'd expect. That may be due to nested object structures, and one way I like to make sure I'm not hallucinating is by using a helper function to log the nested data... just to be sure. ๐Ÿ˜’

Import the following module into your code and you'll be able to logNestedObjects and see any "hidden" information that is stashed away in your response object.

export function logNestedObjects(variable: any, depth = 0) {
    const indent = '  '.repeat(depth); // Adjust the indentation level

    if (typeof variable === 'object' && variable !== null) {
        console.log(indent + '{');
        for (const key in variable) {
            console.log(indent + `  ${key}:`);
            logNestedObjects(variable[key], depth + 2); // Recursively log nested objects
        }
        console.log(indent + '}');
    } else {
        console.log(indent + variable);
    }
}
go back