2015-02-23 104 views
0

我想從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); devices.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:[email protected]/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) 
    } 
} 

在上面做了我要當devices.links.next返回allProducts = null是真正的即未來= NULL。目前。然後功能不起作用。我正在使用q模塊。

感謝您的幫助。

+0

https://stackoverflow.com/questions/23803743/what-is-the-deferred-antipattern-and-how-do-i-avoid-it – Bergi 2015-02-23 12:42:41

+0

'allProducts'初始化在哪裏? – Bergi 2015-02-23 12:44:12

回答

1

只需要一個改變。

return getAllNodeData(currentPage,url,key) 

與下列替換以上getAllNodes線,一切都會去好嗎

getAllNodeData(currentPage,url,key).then(function(){ 
deferred.resolve(allProducts) 
}); 

快樂幫助!

+0

如果你包裝httprequest來返回promise,你可以保留getAllNodeData的返回值,這會使代碼更容易理解。 – 2015-02-23 12:58:17

+0

是的,你[真的應該](http://stackoverflow.com/q/23803743/1048572)這樣做。 @Zeeshan你可以請示麼? – Bergi 2015-02-23 14:12:28

0

你的問題是在nodeback行

return getAllNodeData(currentPage,url,key) 

。從那裏你不能return,只返回then回調的作品。您也可以使用deferred antipattern。相反,promisify只有httprequest功能,並從此只使用承諾。在你的情況下,該功能應該是這樣的:

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; 
    } 
}