使用 NextToken 使用 javascript aws-sdk 解析下一个结果
Use NextToken to parse next results using javascript 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',
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)
}
}
});
}
我是这样做的,没有转到请求中的下一个标记,只是为带有异步等待语法的 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;
}
我是 JavaScript 的新手。我是运行以下请求;
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)
}
}
});
}
我是这样做的,没有转到请求中的下一个标记,只是为带有异步等待语法的 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;
}