Re: [nodejs] update from bd with success but returns undefined to my ontroller in Node.js

10 views
Skip to first unread message
Message has been deleted

DaneiL

unread,
May 30, 2017, 2:39:30 PM5/30/17
to nod...@googlegroups.com
I think, it is happening, because you are calling the funcion done() before the promise resolution.
In servicesDatabase.js 

On 30 May 2017 at 11:44, Catia Matos <cma...@wavecom.pt> wrote:

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...

Link to stackoverflow: https://stackoverflow.com/questions/44264889/update-from-bd-with-success-but-returns-undefined-on-controller-node-js

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/ee5ecffb-0bba-46b3-921d-6eaa5da39230%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
[]'s
Reply all
Reply to author
Forward
0 new messages