跳到主内容

How can I get the full object in Node.js's console.log(), rather than '[Object]'?

· 1分钟阅读

使用 调试时console.log(),如何获取完整对象?

const myObject = {
a: "a",
b: { c: "c", d: { e: "e", f: { g: "g", h: { i: "i" } } } },
};
console.log(myObject);

输出:

{ a: 'a', b: { c: 'c', d: { e: 'e', f: [Object] } } }

但我也想看看f的具体内容

解决方法

你需要使用util.inspect()

const util = require("util");
console.log(util.inspect(myObject, { showHidden: false, depth: null, colors: true }));
// 或者
console.log(util.inspect(myObject, false, null, true /* enable colors */));

输出

{ a: 'a',  b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }

请参阅util.inspect()文档