gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:02:32 26 4
gpt4 key购买 nike

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

这是一个针对基本 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 }

更新

由于我显然没有尽可能清楚,我如何才能取消引用循环引用的元素?它总是父元素吗?

另外值得注意的是,昨晚我了解了 Utils 类和检查方法。这可能会给我提供比我合理地想知道的更多的信息;)

最佳答案

这是一个循环引用。就是这样。

示例:

a.b = a

现在记录一下:

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

}
}
}
}

它永远不会结束,log 调用会产生堆栈溢出,仅此而已。 Node 检测到这些并给出循环引用文本。

关于node.js - 在 Nodejs 中,当我 console.log 一个 req 对象时, [Circular] 引用了什么?如何确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6010173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com