从不同 API 收集数据并在响应中将它们一起发送

Collection data from different API and sending them all together in a response

我想从 API

的多个页面中检索产品

喜欢

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

每个页面都有下一个 API 的 link,如下所示: var devices = JSON.parse(body); devices.links.next

我想从所有页面中检索所有数据。而且我还想在调用所有数据的时候再调用一个函数

我的代码:

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)
    }
}

在上面完成的过程中,当 devices.links.next != null 为真时,我想 return allProducts,即 next = null。目前 .then 功能不工作。我正在使用 q 模块。

感谢您的帮助。

只需更改一处。

return getAllNodeData(currentPage,url,key)

将 getAllNodes 中的上面一行替换为下面一行,一切都会好起来的

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

帮助愉快!

你的问题是线路

return getAllNodeData(currentPage,url,key)

在节点返回中。你不能从那里 return,返回只在 then 回调中有效。

此外,您仅使用 deferred antipattern. Instead, promisify 函数 httprequest ,并且从那时起仅使用 promises。在您的情况下,该函数应如下所示:

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;
    }
}