什么样的对象是node-postgres错误? 为什么node的console.log和JSON.stringify以不同的方式处理它?

console.log像这样输出,

{ [error: syntax error at or near "step"] length: 86, name: 'error', severity: 'ERROR', code: '42601', detail: undefined, hint: undefined, position: '62', internalPosition: undefined, internalQuery: undefined, where: undefined, file: 'scan.l', line: '1001', routine: 'scanner_yyerror' } 

但是JSON.stringify没有看到错误的叙述部分,

{ “长度”:86, “名称”: “错误”, “严重性”: “ERROR”, “代码”: “42601”, “位置”: “62”, “文件”: “scan.l里”,”行 “:” 1001" , “常规”: “scanner_yyerror”}

我不知道如何得到这个“错误:列”未定义“不存在”阅读wikies( https://github.com/brianc/node-postgres/wiki/Error-handling,http:// nodejs。 ru / doc / v0.4.x / stdio.html#console.log )

代码是这样的,

  client.query(selstring, function(err, result) { if(err){ res.send(JSON.stringify(err)); console.log(err); }else{ 

谢谢

更新: err.toString()显示error: syntax error at or near "step"

因为pg错误是标准Js Error()obj的子类,所以可以通过err.message访问基本错误“description”。

 console.log(new Error("hello world")); [Error: hello world] console.log(new Error("hello world").message); hello world 

通过附加Error.prototype,您可以为自己的错误objs子类化标准的Error对象

请参阅Kevin的文章如何在JavaScript中创build自定义错误?

第一步把控制台

的console.log(query.text);

另请参阅可能使用的链接

http://dailyjs.com/2011/09/26/heroku/

简单的解决方法是将端口参数添加到连接中,即使它是默认端口。 我有完全相同的问题。 我发现这个github问题的解决scheme。

 var pg_conn_conf = { username: 'user', pass: 'pass', port: 5432, host: 'localhost', db: 'mydb' }; 

还有另一个stackoverflow的答案 ,build议扩展Error.prototype,幸运的是你不必在这种情况下处理。 但这是一个很好的把戏。

我一直在努力了解很长一段时间同样的事情。

看着JSON.stringify(err)的输出,看起来对象有一个数组作为一个未命名的属性。 如何创build一个具有匿名属性的对象? 我曾经搞砸了很长一段时间,但是找不到任何方法来构build任何可能从JSON.stringify生成类似结构的JSON.stringify

我发现err.message实际上是用来访问string的,但是我真的不明白为什么,因为'消息'在string化版本中并不作为属性出现。 如果'message'是err的一个属性,为什么不出现在stringify的输出中呢? 如果不是,那么它从哪里来?