从不同的API收集数据,并将它们一起发送到响应中
Collection data from different API and sending them all together in a response
我想从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;
}
}
相关文章:
- 转义符不能与innerHTML一起使用
- 如何将getJson的响应保存在全局变量中
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 使用angularjs向浏览器发送servlet响应(下载功能)
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 不再使用innerHTML进行AJAX响应.(代码未得到响应)
- 高亮显示与数组字符串一起使用时文本插件中断
- 为什么可以't我使用了AJAX响应的一部分
- setTimeout可以与闭包内的函数一起使用吗
- 响应动画手风琴不工作
- 将依赖外部库的UMD模块与browserfy捆绑在一起
- Highcharts colorsByPoint与系列中的线性渐变一起使用时不起作用
- 如何从SeleniumWebdriver获取异步Javascript响应
- 如何使用AngularJS将if语句conditon与json响应一起使用
- 如何使用bluebird将原始数据与Promise.map的响应一起传递
- 从不同的API收集数据,并将它们一起发送到响应中
- 响应未与 JavaScript 中的标头一起获取
- 响应标头随Angular一起消失
- GET响应不能与jQuery一起工作
- Jquery的Html响应不能与javascript一起工作