从不同页面收集数据以形成数据列表,并将它们一起发送
Collection data from different pages for a list of data and sending them together
我有一个发布者数组,每个发布者都有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()
}
乐于助人!
相关文章:
- Javascript-如何让脚本与Ajax请求的数据一起运行
- 如何将数据标签与数据一起传递,以便在高图中的工具提示中显示
- 如何使用xmlhttprequest将图像和其他数据一起发送
- 在Javascript中,如何区分与实际数据一起保存的对象和作为引用保存的对象
- 如何将 .find() 与我们使用 .get() 的数据一起使用
- 如何将自定义网址与余烬数据一起使用
- 角度ng重复传递项以与父数据一起使用
- 使d3力布局与标记数据一起工作
- 如何将角度图与json数据一起使用
- 如何使用Ajax将javascript数组与其他非数组数据一起发送到PHP脚本
- 将函数与附加数据一起传递给sails.js
- 通过post与其他数据一起传递json对象
- 如何将文件与其他数据一起发送到webmethod ?不使用任何插头
- 访问与Redux状态数据一起发送给组件的props
- 文件输入数据未与Dropzone表单的POST数据一起传输
- 在MV*设计模式中,模型是单个数据单元还是与数据一起工作的所有逻辑
- 将Meteor中的list.js与从服务器发布到客户端的数据一起使用
- 如何使用xmlhttprequest使一个新的子节点与php脚本中的节点数据一起出现在html中
- 显示数据和单选按钮,并将单选按钮的选定值与数据一起保存
- 以前的表单数据与当前数据一起发布