在Nodejs中,当我在console.log中有一个req对象时,[Circular]引用了什么?如何确定

时间:2022-06-01 16:48:07

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

在Nodejs中,当我在console.log中有一个req对象时,[Circular]是什么意思?

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?

这是针对基本nodejs示例的示例console.log(req)。请注意,request.socket._readWatcher.socket是[Circular]。这是否意味着它指的是自己?我怎样才能取消引用?

{ 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 ;)

另外值得注意的是,昨晚我了解了Utils类和检查方法。这可能会给我更多关于这方面的信息,而不是我应该知道的;)

3 个解决方案

#1


8  

[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.

并且_idleNext和_idlePrev也指向外部的。

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

#2


13  

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 and, the log call will produce a stack overflow and that's it. Node detects those and gives out the circular reference text instead.

它永远不会,并且,日志调用将产生堆栈溢出,就是这样。节点检测到这些并发出循环引用文本。

#3


2  

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

request.socket._readWatcher.socket == request.socket同样适用于其他[Circular]引用。

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

您不应该取消引用它们,因为它会导致未定义的行为(具有套接字的对象需要该引用才能正常工作)。

#1


8  

[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.

并且_idleNext和_idlePrev也指向外部的。

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

#2


13  

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 and, the log call will produce a stack overflow and that's it. Node detects those and gives out the circular reference text instead.

它永远不会,并且,日志调用将产生堆栈溢出,就是这样。节点检测到这些并发出循环引用文本。

#3


2  

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

request.socket._readWatcher.socket == request.socket同样适用于其他[Circular]引用。

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

您不应该取消引用它们,因为它会导致未定义的行为(具有套接字的对象需要该引用才能正常工作)。