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

前端 未结 17 2933
一向
一向 2020-11-22 02:59

When debugging using console.log(), how can I get the full object?

const myObject = {
   \"a\":\"a\",
   \"b\":{
      \"c\":\"c\",
      \"d\":         


        
17条回答
  •  被撕碎了的回忆
    2020-11-22 03:28

    You can simply add an inspect() method to your object which will override the representation of object in console.log messages

    eg:

    var myObject = {
       "a":"a",
       "b":{
          "c":"c",
          "d":{
             "e":"e",
             "f":{
                "g":"g",
                "h":{
                   "i":"i"
                }
             }
          }
       }
    };
    myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
    

    then, your object will be represented as required in both console.log and node shell

提交回复
热议问题