2016-05-03 22 views
0

我是JavaScript新手。我正在運行以下請求;使用NextToken使用JavaScript解析下一個結果aws-sdk

ec2.describeSpotPriceHistory(params, function(err, data) { 
    if (err) console.log(err, err.stack); 
    else  console.log(data); 
}); 

它返回一個json blob;

{ SpotPriceHistory: 
    [ { InstanceType: 'm3.medium', 
     ProductDescription: 'Linux/UNIX', 
     SpotPrice: '0.011300', 
     Timestamp: Tue May 03 2016 18:35:28 GMT+0100 (BST), 
     AvailabilityZone: 'eu-west-1c' }], 
    NextToken: 'cVmnNotARealTokenYcXgTockBZ4lc' } 

這一切都很好。我知道我需要使用NextToken並循環返回以獲得接下來的100個結果。我怎麼能做到這一點?

回答

2

您需要將令牌設置爲params對象中的NextToken屬性,並再次調用describeSpotPriceHistory

事情是這樣的:

function getSpotPriceHistory(params) { 
    ec2.describeSpotPriceHistory(params, function(err, data) { 
     if (err) console.log(err, err.stack); 
     else  { 
      console.log(data); 
      if (data.nextToken) { 
       params.nextToken = data.nextToken; 
       getSpotPriceHistory(params) 
      }     
     } 
    }); 
} 
+0

我如何採取從JSON輸出的'NextToken'值,然後再次傳遞到指揮? –

+0

@ Ste-3PO我在答案中加了一個例子 –