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!

Problem courtesy of: bluekeys

Node.js Recipes责编内容来自:Node.js Recipes (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » How do i access this javascript value?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录