使用NextToken使用javascript aws sdk解析下一个结果
我是JavaScript新手。我正在运行以下请求使用NextToken使用javascript aws sdk解析下一个结果,javascript,amazon-web-services,aws-sdk,Javascript,Amazon Web Services,Aws Sdk,我是JavaScript新手。我正在运行以下请求 ec2.describeSpotPriceHistory(params, function(err, data) { if (err) console.log(err, err.stack); else console.log(data); }); 它返回一个json blob,其中包含: { SpotPriceHistory: [ { InstanceType: 'm3.medium', Prod
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个结果。如何实现这一点?您需要将令牌设置为
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)
}
}
});
}
您需要将令牌设置为
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)
}
}
});
}
我就是这样做的,没有进入请求中的下一个令牌,只是使用异步等待语法为nextToken添加了一个while循环
async function fetchCurrentUsersInGroup(groupName) {
let users = [];
let response = {};
let params = {
GroupName: groupName,
UserPoolId: 'XXXX',
Limit: 60
};
response = await cognitoidentityserviceprovider.listUsersInGroup(params).promise();
users = [...users, ...response.Users];
while(response.NextToken) {
params.NextToken = response.NextToken;
response = await cognitoidentityserviceprovider.listUsersInGroup(params).promise();
users = [...users, ...response.Users];
}
return users;
}
我就是这样做的,没有进入请求中的下一个令牌,只是使用异步等待语法为nextToken添加了一个while循环
async function fetchCurrentUsersInGroup(groupName) {
let users = [];
let response = {};
let params = {
GroupName: groupName,
UserPoolId: 'XXXX',
Limit: 60
};
response = await cognitoidentityserviceprovider.listUsersInGroup(params).promise();
users = [...users, ...response.Users];
while(response.NextToken) {
params.NextToken = response.NextToken;
response = await cognitoidentityserviceprovider.listUsersInGroup(params).promise();
users = [...users, ...response.Users];
}
return users;
}
如何从JSON输出中获取
NextToken
值并将其再次传递给命令?@Ste-3PO我在回答中添加了一个示例如何从JSON输出中获取NextToken
值并将其再次传递给命令?@Ste-3PO我在回答中添加了一个示例