Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Nodejs, when I console.log a req object, what does [Circular] reference? How to determine that

In Nodejs, when I console.log a req object, what does [Circular] mean?

Here's an example console.log(req) against a basic nodejs example. Notice the request.socket._readWatcher.socket is a [Circular]. Does that mean it refers to itself? How can I dereference that?

{ socket: 
   { bufferSize: 0,
     fd: 7,
     type: 'tcp4',
     allowHalfOpen: true,
     _readWatcher: 
      { socket: [Circular],
        callback: [Function: onReadable] },
     destroyed: false,
     readable: true,
     _writeQueue: [],
     _writeQueueEncoding: [],
     _writeQueueFD: [],
     _writeQueueCallbacks: [],
     _writeWatcher: 
      { socket: [Circular],
        callback: [Function: onWritable] },
     writable: true,
     _writeImpl: [Function],
     _readImpl: [Function],
     _shutdownImpl: [Function],
     remoteAddress: '127.0.1.1',
     remotePort: 40407,
     server: 
      { connections: 1,
        allowHalfOpen: true,
        watcher: [Object],
        _events: [Object],
        httpAllowHalfOpen: false,
        type: 'tcp4',
        fd: 5 },
     ondrain: [Function],
     _idleTimeout: 120000,
     _idleNext: 
      { repeat: 120,
        _idleNext: [Circular],
        _idlePrev: [Circular],
        callback: [Function] },
     _idlePrev: 
      { repeat: 120,
        _idleNext: [Circular],
        _idlePrev: [Circular],
        callback: [Function] },
     _idleStart: Sun, 15 May 2011 01:18:50 GMT,
     _events: 
      { timeout: [Function],
        error: [Function],
        close: [Function] },
     ondata: [Function],
     onend: [Function],
     _httpMessage: 
      { output: [],
        outputEncodings: [],
        writable: true,
        _last: false,
        chunkedEncoding: true,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: true,
        _hasBody: true,
        _trailer: '',
        finished: false,
        socket: [Circular],
        connection: [Circular],
        _events: [Object],
        _header: 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n',
        _headerSent: false } },
  connection: 
   { bufferSize: 0,
     fd: 7,
     type: 'tcp4',
     allowHalfOpen: true,
     _readWatcher: 
      { socket: [Circular],
        callback: [Function: onReadable] },
     destroyed: false,
     readable: true,
     _writeQueue: [],
     _writeQueueEncoding: [],
     _writeQueueFD: [],
     _writeQueueCallbacks: [],
     _writeWatcher: 
      { socket: [Circular],
        callback: [Function: onWritable] },
     writable: true,
     _writeImpl: [Function],
     _readImpl: [Function],
     _shutdownImpl: [Function],
     remoteAddress: '127.0.1.1',
     remotePort: 40407,
     server: 
      { connections: 1,
        allowHalfOpen: true,
        watcher: [Object],
        _events: [Object],
        httpAllowHalfOpen: false,
        type: 'tcp4',
        fd: 5 },
     ondrain: [Function],
     _idleTimeout: 120000,
     _idleNext: 
      { repeat: 120,
        _idleNext: [Circular],
        _idlePrev: [Circular],
        callback: [Function] },
     _idlePrev: 
      { repeat: 120,
        _idleNext: [Circular],
        _idlePrev: [Circular],
        callback: [Function] },
     _idleStart: Sun, 15 May 2011 01:18:50 GMT,
     _events: 
      { timeout: [Function],
        error: [Function],
        close: [Function] },
     ondata: [Function],
     onend: [Function],
     _httpMessage: 
      { output: [],
        outputEncodings: [],
        writable: true,
        _last: false,
        chunkedEncoding: true,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: true,
        _hasBody: true,
        _trailer: '',
        finished: false,
        socket: [Circular],
        connection: [Circular],
        _events: [Object],
        _header: 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n',
        _headerSent: false } },
  httpVersion: '1.1',
  complete: false,
  headers: 
   { 'user-agent': 'curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18',
     host: 'uh-uh-uh-I-aint-telling',
     accept: '*/*' },
  trailers: {},
  readable: true,
  url: '/',
  method: 'GET',
  statusCode: null,
  client: 
   { bufferSize: 0,
     fd: 7,
     type: 'tcp4',
     allowHalfOpen: true,
     _readWatcher: 
      { socket: [Circular],
        callback: [Function: onReadable] },
     destroyed: false,
     readable: true,
     _writeQueue: [],
     _writeQueueEncoding: [],
     _writeQueueFD: [],
     _writeQueueCallbacks: [],
     _writeWatcher: 
      { socket: [Circular],
        callback: [Function: onWritable] },
     writable: true,
     _writeImpl: [Function],
     _readImpl: [Function],
     _shutdownImpl: [Function],
     remoteAddress: '127.0.1.1',
     remotePort: 40407,
     server: 
      { connections: 1,
        allowHalfOpen: true,
        watcher: [Object],
        _events: [Object],
        httpAllowHalfOpen: false,
        type: 'tcp4',
        fd: 5 },
     ondrain: [Function],
     _idleTimeout: 120000,
     _idleNext: 
      { repeat: 120,
        _idleNext: [Circular],
        _idlePrev: [Circular],
        callback: [Function] },
     _idlePrev: 
      { repeat: 120,
        _idleNext: [Circular],
        _idlePrev: [Circular],
        callback: [Function] },
     _idleStart: Sun, 15 May 2011 01:18:50 GMT,
     _events: 
      { timeout: [Function],
        error: [Function],
        close: [Function] },
     ondata: [Function],
     onend: [Function],
     _httpMessage: 
      { output: [],
        outputEncodings: [],
        writable: true,
        _last: false,
        chunkedEncoding: true,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: true,
        _hasBody: true,
        _trailer: '',
        finished: false,
        socket: [Circular],
        connection: [Circular],
        _events: [Object],
        _header: 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n',
        _headerSent: false } },
  httpVersionMajor: 1,
  httpVersionMinor: 1,
  upgrade: false }

UPDATE

Since I apparently wasn't as clear as I could have been, how can I dereference what element is being referenced circularly? Is it always the parent element?

Also of note, last night I learned about the Utils class, and the inspect method. Which will probably give me way more information about this than I should reasonably want to know ;)

like image 469
jcolebrand Avatar asked May 15 '11 17:05

jcolebrand


People also ask

What is circular in node JS?

A circular dependency occurs when two classes depend on each other.

How does console log work in node?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

What are circular objects in Javascript?

A circular reference occurs if two separate objects pass references to each other. In older browsers circular references were a cause of memory leaks. With improvements in Garbage collection algorithms, which can now handle cycles and cyclic dependencies fine, this is no longer an issue.


2 Answers

It's a circular reference. That's all.

Example:

a.b = a

Now log it:

[Object a] {
    b: [Object a] {
        b: [Object a] {
            b: [Object a] {
                b: [Object a] {
                    b: [Object a] {
                        b: [Object a] {
                            ... welcome to recursion!
                        }
                    }
                }

            }
        }
    }
}

It will never end, and the log call will produce a stack overflow and that's it. Node detects those and gives out the circular reference text instead.

like image 135
Ivo Wetzel Avatar answered Oct 13 '22 17:10

Ivo Wetzel


[Circular] simply means circular reference.

var o = {
    "self": o
}

Is shown as

{
    "self": [Circular]
}

It could be shown as

{
    "self": {
         "self": {
              "self": {
                   ...
              }
         }
    }
}

In your case socket is the outer socket.

And _idleNext and _idlePrev also point to the outer ones.

Update

_idleNext: // This one!
  { repeat: 120,
    _idleNext: [Circular],



{ socket: // this one!
   { bufferSize: 0,
     fd: 7,
     type: 'tcp4',
     allowHalfOpen: true,
     _readWatcher: 
      { socket: [Circular],
        callback: [Function: onReadable] },
     destroyed: false,
     readable: true,
     _writeQueue: [],
     _writeQueueEncoding: [],
     _writeQueueFD: [],
     _writeQueueCallbacks: [],
     _writeWatcher: 
      { socket: [Circular],
like image 33
Raynos Avatar answered Oct 13 '22 16:10

Raynos