bluekeys
bluekeys

Reputation: 2267

How do i access this javascript value?

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!

Upvotes: 1

Views: 166

Answers (1)

bluekeys
bluekeys

Reputation: 2267

The answer to the question was very simple.

error.toString();

False alarm.

Upvotes: 2

Related Questions