gpt4 book ai didi

javascript - 从不同页面收集数据形成数据列表并一起发送

转载 作者:行者123 更新时间:2023-12-03 10:42:15 25 4
gpt4 key购买 nike

我有一个发布者数组,每个发布者都有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.motionEvent数组中。并且我还想在收集所有发布者的所有数据时调用另一个函数。并且 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')
}
}

为那些具有 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()
}

很高兴为您提供帮助!

关于javascript - 从不同页面收集数据形成数据列表并一起发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28721937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com