从不同的页面收集数据以获取数据列表并将它们一起发送

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

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

'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.motion事件array.And我还想在为所有发布者收集所有数据时调用另一个函数。并且 Publisher 被推入 allProduct 数组

我的代码:

getEventsOfPublishes(objectList,'M')

}).then(function(allPublisherWithMotionEvents){
    console.log(allPublisherWithMotionEvents)
})

此处的 objectList 是所有具有 motionID 的发布者 属性。

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

为那些有动作 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 未返回承诺,请将其替换为以下代码:

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

帮助愉快!