开发者

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,
      开发者_如何学C  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 ;)


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.


[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],


request.socket._readWatcher.socket == request.socket Similarly for the other [Circular] references.

You should not dereference them, as it will result in undefined behaviour (the objects that have sockets need that reference to work properly).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜