Friday, 26 May 2017

UPDATE PUT rowCount:0 nodejs

Hy! I'm trying tp update a json field on my bd that already have information like {"status":true,"interfaces":[{"ifname":"lte0","enabled":"1"}],"sn":"IN-3M-1083984","name":"41-SI-78"} and I want to add a new object called "map" which have {"username":"teste@internorte","isRoot":"true","visible":"offline"}.

So my goal is to have a json with {"status":true,"interfaces":[{"ifname":"lte0","enabled":"1"}],"sn":"IN-3M-1083984","name":"41-SI-78", "map": {"username":"teste@internorte","isRoot":"true","visible":"offline"}}

But when I try to update this row on bd i always get updateRow result {"command":"UPDATE","rowCount":0,"oid":null,"rows":[],"fields":[],"_parsers":[],"RowCtor":null,"rowAsArray":false}

My code is:

ServicesDatabase.prototype.addMapInfo = function (data, deviceId) {

    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo','begin');
    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo','deviceId', deviceId);
    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo','data', data);

    return new Promise(function (resolve, reject) {
        createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo','depois da promise');
        pg.connect(dbStatsConnectionString, function (err, client, done) {
            if (err) {
                createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo', err);
                reject(err);
                return
            }
            client.query("UPDATE device_services SET data=jsonb_set(data::jsonb,'{map}',$1::jsonb,true), modified_date=NOW() WHERE device_id=$2", [data, deviceId], function (err, result) {
                done();
                createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo','done');
                createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo','result', result);
                if (err) {
                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo', err);
                    reject(err);
                } else {
                    resolve(result.rows[0]);
                    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo','else resolve');
                }
            });
        });
    });
        createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'addMapInfo','end');
}

My table have the columns id (uuid), data (json) and modified_date (timestamp without timezone) and I'm using pg.



via Catia Matos

No comments:

Post a Comment