0

I want to retrieve product from multiple pages of the API

like

https://example.com/v2/nodes/?resource__type=device&page=1 
https://example.com/v2/nodes/?resource__type=device&page=2
.
.

Each page have the link for next API like this: var devices = JSON.parse(body); devices.links.next

I want to retrieve all the data from all page. And I also want to call another function when all data is called.

my Code:

getAllNodeData(1,"https://example/v2/nodes/?resource__type=device&page=", 'A').then(function(objectList){

    console.log('--------')
    console.log(allProducts.length)
})

function getAllNodeData(currentPage,url,key){

    var deferred = Q.defer();
    var result
    httprequest(url+currentPage,
        function(err, res, body) {
            var devices = JSON.parse(body);
            var next;
            var tempDeviceObject = {}
            //console.log(devices)
            saveProducts(devices.objects,key)
            if(devices.links.next != null){
                currentPage++
                return getAllNodeData(currentPage,url,key)
            }else{
                console.log('I am here')
                result =  deferred.resolve(allProducts);
            }
           // if(devices.totalObjects == allProducts.length){

            //}

        })

    return deferred.promise;
}



function saveProducts(objects,key){
    if(key === 'A'){

        objects.forEach(function (device) {
            var tempDeviceObject = {}
            tempDeviceObject.id = device.uid
            tempDeviceObject.name = device.label
            tempDeviceObject.type = device.resource.slug
            device.publishes.forEach(function(pub){
                if((pub.label=== 'Motion') && (pub.type.toLowerCase() === 'motion')){
                    var currentPage = 1;
                    var key = 'M';
                    var url = "https://crossoft:snSprynet0@apis.sen.se/v2/feeds/"+pub.uid+"/events/?page=";
                    tempDeviceObject.motion =pub.uid
                 //  return  getEventsOfPublishes(pub.uid,url,key,currentPage)
                }else if((pub.label=== 'Battery') && (pub.type.toLowerCase() === 'battery')){
                    tempDeviceObject.battery =pub.uid
                }else if((pub.label=== 'Temperature') && (pub.type.toLowerCase() === 'temperature')){
                    tempDeviceObject.temperature =pub.uid
                }
            })

            allProducts.push(tempDeviceObject)

        })
        return allProducts
        //console.log(allProducts.length)
    }
}

In the above done I want to return allProducts when devices.links.next != null is true i.e next = null. Currently .then function is not working . I am using q module.

Thanks for Your help.

2 Answers2

1

Its one change required only.

return getAllNodeData(currentPage,url,key)

replace above line in getAllNodes with following one, and all will go okay

getAllNodeData(currentPage,url,key).then(function(){
 deferred.resolve(allProducts)
});

Happy Helping!

Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57
  • you can keep also make use of return getAllNodeData if you wrap httprequest to return promise, it will make code easier to understand. – Zeeshan Hassan Memon Feb 23 '15 at 12:58
  • Yes, you [really should](http://stackoverflow.com/q/23803743/1048572) do that. @Zeeshan can you please show how? – Bergi Feb 23 '15 at 14:12
0

Your problem is the line

return getAllNodeData(currentPage,url,key)

in the nodeback. You cannot return from there, returning only works in then callbacks.

Also, you are using the deferred antipattern. Instead, promisify only the httprequest function, and from then on use only promises. In your case, the function should look like this:

var promiseRequest = Q.nfbind(httprequest);

function getAllNodeData(currentPage, url, key) {
    return getNodeData(currentPage, []);

    function getNodeData(currentPage, allProducts) {
        return promiseRequest(url+currentPage).then(function(res, body) {
            var devices = JSON.parse(body);
            var tempDeviceObject = {}
            allProducts = saveProducts(devices.objects, allProducts)
            if (devices.links.next != null) {
                return getNodeData(currentPage+1, allProducts)
            } else {
                console.log('I am here')
                return allProducts;
            }
        });
    }
    function saveProducts(objects, allProducts) {
        if (key === 'A') {
            objects.forEach(function (device) {
                var tempDeviceObject = {
                    id: device.uid,
                    name: device.label,
                    type: device.resource.slug
                };
                device.publishes.forEach(function(pub) {
                    if ((pub.label==='Motion') && (pub.type.toLowerCase()==='motion')) {
                        tempDeviceObject.motion = pub.uid;
                    } else if ((pub.label==='Battery') && (pub.type.toLowerCase()==='battery')) {
                        tempDeviceObject.battery = pub.uid;
                    } else if ((pub.label==='Temperature') && (pub.type.toLowerCase()==='temperature')) {
                        tempDeviceObject.temperature = pub.uid;
                    }
                });
                allProducts.push(tempDeviceObject);
           });
        }
        return allProducts;
    }
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375