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

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

本文关键字:一起 响应 API 数据      更新时间:2024-03-15

我想从API 的多个页面中检索产品

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

每个页面都有下一个API的链接,如下所示:var devices=JSON.parse(body);dell.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)
    }
}

在上面完成的操作中,我希望在以下时间返回所有产品:null为true,即next=null。当前。则功能不起作用。我正在使用q模块。

谢谢你的帮助。

只需要对其进行一次更改。

return getAllNodeData(currentPage,url,key)

将getAllNodes中的上面一行替换为下面一行,所有的都会正常运行

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

乐于助人!

您的问题是线路

return getAllNodeData(currentPage,url,key)

在nodeback中。不能从那里返回return,返回仅适用于then回调。

此外,您正在使用延迟的反模式。相反,promise只使用httprequest函数,从此以后只使用promise。在您的情况下,函数应该如下所示:

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