Tuesday, 9 May 2017

How to do api call request to get data of a set of objects in nodeJs

Giving a partner API backend, where are stored un set of object with their informations. I can get by an api request information of one object in format JSON. My problem is how can I get all information of all objects. The code below is built to get information of one object:

module.exports.getBackendData = function (request, response) {
    var deviceid = request.params.id_device;
    var id = request.params._id;
    var points = [];
    var optionsget = {
        host: 'ws-json.traqueur.fr', //here only the domain name
        port: 443
        , path: '/api/partner/' + id + '', //the rest of the url parameters if needed 
        method: 'GET', //do GET
        auth: 'username:password'
    };
    var myRequest = https.request(optionsget, function (myResponse) {
        var myData = '';

        myResponse.on('data', function (d) {
            myData += d;
        });

        myResponse.on('end', function () {
            str = JSON.parse(myData);
            var lat, long, statut, timestamp;
            for (var i = 0; i <= str.reports.length - 1; i++) {
                lat = str.reports[i].latitude;
                long = str.reports[i].longitude;
                timestamp = str.reports[i].ts;
                statut = str.reports[i].move;

                locations.push({
                    long: long
                    , latitude: lat
                    , reportid: reportid
                    , move: statut
                    , timestamp: timestamp
                });
            }

            response.json(points);
        });
    });
    myRequest.end();
    myRequest.on('error', function (e) {
        console.error("error:", e);
    });
}

As api call is done asynchronously I don't know how to get one time response of all objects.



via Blackhat

No comments:

Post a Comment