What kind of object is node-postgres Error? Why node's console.log and JSON.stringify handle it differently?

console.log outputs it like this,

{ [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' }

but JSON.stringify doesn't sees the narrative part of an error,

{"length":86,"name":"error","severity":"ERROR","code":"42601","position":"62","file":"scan.l","line":"1001","routine":"scanner_yyerror"}

I can't figure out how to get this "error: column "undefined" does not exist" reading wikies (https://github.com/brianc/node-postgres/wiki/Error-handling, http://nodejs.ru/doc/v0.4.x/stdio.html#console.log)

The code is like this,

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

thanks

UPDATE: err.toString() shows error: syntax error at or near "step"

Because the pg error is a subclass of the standard Js Error() obj and therefore the base error "description" is accessible via err.message...

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

You can subclass the standard Error object for your own error objs by appending the Error.prototype

See Kevin's post How do I create a custom Error in JavaScript?

First step put console like

console.log(query.text);

Also refer the link may use

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

The easy fix is to add port parameter into your connection, even if it is the default port. I had the exact same issue. I found out about the solution from this github issue.

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

There is also another stackoverflow answer that suggests extending the Error.prototype, luckily you don't have to deal with that in this case. But it is a good trick to know.

I've been struggling to understand the same thing for a long time now.

Looking at the output of JSON.stringify(err), it appears that the object has an array as an unnamed attribute. How is it even possible to create an object with an anonymous attribute? I once messed with this for quite a while but could find no way to even construct any object that could generate a similar structure from JSON.stringify.

I've found err.message actually works to access the string, but I really don't understand why, as 'message' doesn't appear as an attribute in the stringified version. If 'message' is an attribute of err, why doesn't it show up in the output from stringify? If it isnt, then where is it coming from?