1

I'm getting a javascript error returned from postgres using node 'pg'.

Here is the code the generates the error

How the object is generated.

function storeDeviceState(ingres_id, info) {
        var deviceId;
        var insertDevice = client.query({
                text: "INSERT INTO devices (serialNumber, imeiNumber) VALUES ($1, $2)",
                values: [info.serialNumber, info.imeiNumber]
        }, function (error, results) {
                if (error) {
                        // [error: duplicate key value violates unique constraint "devices_serialnumber_key"
                        console.log(error);
                } else {
                        deviceId = results.rows[0].id;
                        console.log(results);
                }
        });
}

Below is a copy and paste of what I see in the console as a result of console.log(error);

{ [error: duplicate key value violates unique constraint "devices_serialnumber_key"]
  length: 130,
  name: 'error',
  severity: 'ERROR',
  code: '23505',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'nbtinsert.c',
  line: '300',
  routine: '_bt_check_unique' }

Here's what I've tried,

error[0];
error[0].error;
error.error;

I can access the code for example by

error.code;

Here are the results of JSON.stringify on the object.

{"length":130,"name":"error","severity":"ERROR","code":"23505","file":"nbtinsert.c","line":"300","routine":"_bt_check_unique"}

Please help!

10
  • 3
    well first off, that is not a valid JSON object Commented Feb 22, 2012 at 20:05
  • 1
    that isn't a valid JS object, btw. (oh 20 seconds too late) =) Commented Feb 22, 2012 at 20:05
  • 1
    Are you 100% sure this is what pg returns? It does not look like correctly formatted JSON. Mainly the first square bracket should not be where it is. json.org Ops, too late ;) Commented Feb 22, 2012 at 20:06
  • That totally looks as if the server side errors out while trying to generate a JavaScript (JSON) object. Commented Feb 22, 2012 at 20:06
  • 1
    @Ege, the answer to the question is to use error.toString(); lol! Commented Feb 22, 2012 at 20:28

1 Answer 1

2

The answer to the question was very simple.

error.toString();

False alarm.

Sign up to request clarification or add additional context in comments.

3 Comments

If someone else wants to answer this for me, I'll accept their answer.
self answers are fine. Instead, if you like, we can delete the entire question.
I'll leave it up there and accept my own answer in a couple of days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.