Thursday, 1 June 2017

error: invalid input syntax for type json Node.js

Hy everyone! I'm clicking in a button and send a ajax request to a rest api where I send a status and the name of what i want to change and then I want to update a value in my db using the fields: "63320169-611e-43f5-880e-9b1a13152cfd" "lte" true

ServicesDatabase.prototype.updateDeviceInterface = function (deviceId, ifname, status) {
    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'updateDeviceInterface','begin');
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'updateDeviceInterface','deviceId', deviceId);
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'updateDeviceInterface','ifname', typeof(ifname));
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'updateDeviceInterface','status', typeof(status));

    return new Promise(function (resolve, reject) {
        pg.connect(dbStatsConnectionString, function (err, client, done) {
            if (err) {
                createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'updateDeviceInterface', err);
                reject(err);
                return
            }
            client.query("UPDATE device_services ds \
                    SET data = jsonb_set(   \
                        data,               \
                        '{interfaces}',     \
                        (                   \
                            SELECT  jsonb_agg(CASE WHEN j.value->>'ifname' = $1 THEN j.value || '{\"\enabled\"\: $2}' ELSE j.value END) \
                            FROM jsonb_array_elements(ds.data->'interfaces') AS j(value)                                        \
                        )                   \
                   ) WHERE ds.device_id=$3;", [ifname, status, deviceId], function (err, result) {
                done();
                if (err) {
                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'updateDeviceInterface', err);
                    reject(err);
                } else {
                    resolve(result);
                }
            });
        });
    });
}

Here in my db the fields device_id = uuid and data=jsonb.

Error log:

[2017-06-01 15:32:01.980] - info: /opt/wscontroller/wscontroller-api/routes/services ServicesDatabase 63320169-611e-43f5-880e-9b1a13152cfd updateDeviceInterface begin
2|wscontro | [2017-06-01 15:32:01.981] - debug: /opt/wscontroller/wscontroller-api/routes/services ServicesDatabase 63320169-611e-43f5-880e-9b1a13152cfd updateDeviceInterface deviceId "63320169-611e-43f5-880e-9b1a13152cfd"
2|wscontro | [2017-06-01 15:32:01.981] - debug: /opt/wscontroller/wscontroller-api/routes/services ServicesDatabase 63320169-611e-43f5-880e-9b1a13152cfd updateDeviceInterface ifname "string"
2|wscontro | [2017-06-01 15:32:01.981] - debug: /opt/wscontroller/wscontroller-api/routes/services ServicesDatabase 63320169-611e-43f5-880e-9b1a13152cfd updateDeviceInterface status "boolean"
2|wscontro | [2017-06-01 15:32:01.987] - error: /opt/wscontroller/wscontroller-api/routes/services ServicesDatabase 63320169-611e-43f5-880e-9b1a13152cfd updateDeviceInterface { error: invalid input syntax for type json
2|wscontro |     at Connection.parseE (/opt/wscontroller/wscontroller-api/node_modules/pg/lib/connection.js:539:11)
2|wscontro |     at Connection.parseMessage (/opt/wscontroller/wscontroller-api/node_modules/pg/lib/connection.js:366:17)
2|wscontro |     at Socket.<anonymous> (/opt/wscontroller/wscontroller-api/node_modules/pg/lib/connection.js:105:22)
2|wscontro |     at emitOne (events.js:96:13)
2|wscontro |     at Socket.emit (events.js:188:7)
2|wscontro |     at readableAddChunk (_stream_readable.js:176:18)
2|wscontro |     at Socket.Readable.push (_stream_readable.js:134:10)
2|wscontro |     at TCP.onread (net.js:548:20)
2|wscontro |   name: 'error',
2|wscontro |   length: 163,
2|wscontro |   severity: 'ERROR',
2|wscontro |   code: '22P02',
2|wscontro |   detail: 'Token "$" is invalid.',
2|wscontro |   hint: undefined,
2|wscontro |   position: '178',
2|wscontro |   internalPosition: undefined,
2|wscontro |   internalQuery: undefined,
2|wscontro |   where: 'JSON data, line 1: {"enabled": $...',
2|wscontro |   schema: undefined,
2|wscontro |   table: undefined,
2|wscontro |   column: undefined,
2|wscontro |   dataType: undefined,
2|wscontro |   constraint: undefined,
2|wscontro |   file: 'json.c',
2|wscontro |   line: '1243',
2|wscontro |   routine: 'report_invalid_token' }
2|wscontro | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
2|wscontro | error: invalid input syntax for type json
2|wscontro |     at Connection.parseE (/opt/wscontroller/wscontroller-api/node_modules/pg/lib/connection.js:539:11)
2|wscontro |     at Connection.parseMessage (/opt/wscontroller/wscontroller-api/node_modules/pg/lib/connection.js:366:17)
2|wscontro |     at Socket.<anonymous> (/opt/wscontroller/wscontroller-api/node_modules/pg/lib/connection.js:105:22)
2|wscontro |     at emitOne (events.js:96:13)
2|wscontro |     at Socket.emit (events.js:188:7)
2|wscontro |     at readableAddChunk (_stream_readable.js:176:18)
2|wscontro |     at Socket.Readable.push (_stream_readable.js:134:10)
2|wscontro |     at TCP.onread (net.js:548:20)



via Catia Matos

No comments:

Post a Comment