NodeJS错误堆栈未定义
问题描述:
我正在使用节点检查器,我注意到 new Error()
有未定义的堆栈。
I am using node inspector and I noticed new Error()
had undefined stacks.
如果我已分配这个值变量,该变量将显示堆栈未定义。
If I assigned this value to a variable, that variable would show that the stack is undefined.
有趣的是,运行 new Error()。stack
生成一个包含正确堆栈的消息。
Interestingly enough, running new Error().stack
produces a message with the proper stack.
理想情况下,这些错误默认会有堆栈,所以当我记录时我可以知道在哪里看。
Ideally these errors would have stacks by default so when I log I can know where to look.
我不明白为什么会发生这种情况,无法找到相关信息。我在这里缺少什么?
I do not understand why this is happening and cannot find information on it. Is there something I'm missing here?
答
因为 当访问error.stack属性时,表示堆栈跟踪的字符串 lazily
一个小例子:
function newError() {
var error = new Error('ohhh')
// See the contents of a _stack variable using the Inspector
var _stack = '' + error.stack
}
newError()