从不同页面收集数据以形成数据列表,并将它们一起发送

Collection data from different pages for a list of data and sending them together

本文关键字:数据 一起 列表      更新时间:2024-05-06

我有一个发布者数组,每个发布者都有motionID,这个ID用于从API获取数据,比如

'https://example.com/v2/feeds/'+publisher.motion+'/events/?page='

publisher.motion是motionID。该动议的每个发布者都有多页的事件。

'https://example.com/v2/feeds/'+publisher.motion+'/events/?page=1'
'https://example.com/v2/feeds/'+publisher.motion+'/events/?page=2'
 .
 .
'https://example.com/v2/feeds/'+publisher.motion+'/events/?page=n'

我想收集所有运动页面中的所有事件,并保存到publisher.motionEvent数组中。我还想在为所有发布者收集所有数据时调用另一个函数。和Publisher被推入所有产品阵列

我的代码:

getEventsOfPublishes(objectList,'M')
}).then(function(allPublisherWithMotionEvents){
    console.log(allPublisherWithMotionEvents)
})

here-objectList是所有具有motionID作为there属性的发布者。

function getEventsOfPublishes(objectList,key){
    if(objectList.length){
        if(key == 'M'){
            var promises = objectList.map(getEventsOfMotion);
            return Q.all(promises);
        }
    }else{
        return {msg:"No Objects"};
    }
}

使用q.map为每个发布者调用函数。

function getEventsOfMotion(publisher){
    if(publisher.motion !== undefined){
        //console.log('motion')
        url = 'https://example.com/v2/feeds/'+publisher.motion+'/events/?page=';
        publisher.motionEvent = []
        getAllDataOfEvents(1,url,'M',publisher).then(function(test){
            allProducts.push(test)
        //console.log(test)
        })
    }else{
        console.log('yes')
    }
}

为那些具有motion Id的发布者调用函数"getAllDataOfEvents"。

function getAllDataOfEvents(currentPage,url,key,publisher){
    var deferred = Q.defer();
    var result
    httprequest(url+currentPage,
        function(err, res, body) {
            var events = JSON.parse(body);
            var next;
            var tempDeviceObject = {}
            // console.log(events.objects)
            saveProducts(events.objects,key,publisher)
            if(events.links.next != null){
                currentPage++
                getAllDataOfEvents(currentPage,url,key,publisher).then(function(){
                    deferred.resolve(publisher)
                });
            }else{
                result =  deferred.resolve(publisher);
            } 
       })
    return deferred.promise;
}

function saveProducts(objects,key,publisher){
    if(key === 'M'){
        if(objects){
            //console.log(objects.length+'---------'+publisher.motion)
            objects.forEach(function (event) {
                var tempEventobject = {}
                tempEventobject.date = event.dateEvent;
                tempEventobject.durationSeconds = event.data.durationSeconds
                tempEventobject.numberMovements = event.data.numberMovements
                tempEventobject.avgIntensity = event.data.avgIntensity
                tempEventobject.eventID = publisher.motion
                publisher.motionEvent.push(tempEventobject)
            })
            //console.log(publisher);
        }
    }else{
    //    console.log(publisher)
    }
}

在上面的代码中,所有事件都收集在publisher.motionEvent中,但allPublisherWithMotionEvents是未定义的数组,即函数不等待收集。我使用了Q模块。谢谢你的帮助。

您的代码中有一个小错误,即getEventsOfMotion没有返回promise,请将其替换为以下错误:

function getEventsOfMotion(publisher){
    var defer = q.defer();
    if(publisher.motion !== undefined){
        //console.log('motion')
        url = 'https://example.com/v2/feeds/'+publisher.motion+'/events/?page=';
        publisher.motionEvent = []
        getAllDataOfEvents(1,url,'M',publisher).then(function(test){
            allProducts.push(test);
            defer.resolve();
        //console.log(test)
        })
    }else{
        console.log('yes')
    }
 return defer.promise()
}

乐于助人!