简体   繁体   English

Express应用程序未定义错误对象

[英]Error Object undefined for Express application

I have setup an Express app. 我已经安装了Express应用。 It seems that the global Error object is undefined. 似乎全局Error对象是未定义的。

console.log(Error) gives 'undefined' , and console.log(JSON.stringify(new Error('error message')) gives {} console.log(Error)给出'undefined', console.log(JSON.stringify(new Error('error message'))给出{}

Since Error object is undefined I cannot return errors like this 由于错误对象是未定义的,因此无法返回此类错误

return next( new Error( 'error message!' ) 

Is it possible that Error object is renamed or something? 错误对象是否可能被重命名? Is there a work around this? 有没有解决的办法?

(I used IntelliJ Idea to construct the express app.) (我使用IntelliJ Idea构建了快速应用程序。)

Any help would be appreciated. 任何帮助,将不胜感激。

Error object is not undefined. 错误对象不是未定义的。 If it was, this would throw a TypeError: 如果是这样,则会抛出TypeError:

console.log(JSON.stringify(new Error('error message')));

Instead, it is returning an empty object, because that is what V8 returns if you send an Error object into JSON.stringify() . 相反,它将返回一个空对象,因为如果将Error对象发送到JSON.stringify() ,则V8将返回该对象。 I'm not sure if that's a feature or a bug or neither, but regardless, this will give you the result you expect: 我不确定这是功能还是Bug,还是两者都不是,但是无论如何,这将为您带来预期的结果:

console.log(new Error('error message'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM