Tuesday 30 May 2017

Update from bd with success but returns undefined on Controller Node.Js

Hy everyone, I'm having some troubles with my rest api. I have in my ui a button where I click to update the state of a bus ( visible / not visible). By clicking on the button I can update the state of the item on the map.

So my problem is when I update the info in my DB in my controller i get the result of this as undefined but the resolve of the db returns

{"command":"UPDATE","rowCount":1,"oid":null,"rows":[],"fields":[],"_parsers":[],"RowCtor":null,"rowAsArray":false}

I dont get it...

ServicesController.js

ServicesController.prototype.updateMap = function (req, res, next) {

    var data = req.body;
    if (isEmptyObject(data)) {
        res.status(400).send({error: errorMessage.emptyBody});
        return;
    }

    if (data.sn === undefined || data.sn === "") {
        res.status(400).send({error: "Invalid serial number"});
        return;
    }

    Database.Services.getDeviceBySn(data.sn).then(function (device) {
        var map_data={
            "isRoot": data.root,
            "visible": data.visible
        }

        Database.Services.addMapInfo(map_data, device.id).then(function (map) {
            console.log("updateMap depois do addMapInfo   ---   map >>> ", map);
            if (map) {
                res.status(200).send(map);
            } else {
                res.status(404).end();
            }
        }).catch(function (e) {
            res.status(500).send(e);
        });

    }).catch(function (e) {
        res.status(500).send(e);
    });
}

ServicesDatabase.js

ServicesDatabase.prototype.addMapInfo = function (data, deviceId) {   
    return new Promise(function (resolve, reject) {
        pg.connect(dbStatsConnectionString, function (err, client, done) {
            if (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();
                if (err) {
                    reject(err);
                } else {
                    resolve(result.rows[0]);
                }
            });
        });
    });
}

My parameters are data {"isRoot":"false","visible":"online"} and deviceId "1f110136-9490-4ea5-a46d-3fdfa65ea0ab"

My controller always return 404 because of this

if (map) {
    res.status(200).send(map);
} else {
    res.status(404).end();
}

Anyone can help me? I dont get it...



via Catia Matos

No comments:

Post a Comment