7

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"

fedd
  • 880
  • 12
  • 39
  • Please post the contents of `selstring`. – robertklep Feb 27 '13 at 13:19
  • i dont think it really matters, but here it is: `select max(date)::character varying dt from calendar where11 step=0 `. my task is to get the error description. the query is intentionally wrong – fedd Feb 27 '13 at 13:24
  • 1
    Oh right sorry, I misunderstood your question :( What does `err.toString()` return? – robertklep Feb 27 '13 at 13:24
  • I've been advised to use toString() and it helped. anyway, the best practice method to work with error would be appreciated... – fedd Feb 27 '13 at 13:34
  • @robertklep, oh i didnt notice you mentioned toString too, here's an upvote – fedd Feb 27 '13 at 14:03
  • 1
    Still not quite sure what you're trying to accomplish. If you want the narrative, you can get it with `toString` (EDIT: `err.message` is much cleaner) – robertklep Feb 27 '13 at 14:21
  • just seems a little bit wrong, that `toString()` returns some information that couldn't be got from any field of an object. (I'm new to Node and JS in general) – fedd Feb 27 '13 at 14:27
  • 1
    A class can overload the `toJSON` method to decide for itself what will be returned when you call `JSON.stringify()` on its instances. Here's an example: http://jsfiddle.net/QmWfq/ – robertklep Feb 27 '13 at 14:33

4 Answers4

6

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?

Community
  • 1
  • 1
Paul Coleman
  • 86
  • 1
  • 3
0

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?

cmd
  • 11,622
  • 7
  • 51
  • 61
-1

First step put console like

console.log(query.text);

Also refer the link may use

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

AmirtharajCVijay
  • 1,078
  • 11
  • 12
-1

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.

Community
  • 1
  • 1
SerkanSerttop
  • 588
  • 2
  • 9