我们如何通过调用 dynamodb.query 获取所有项目?
文档指出我们需要寻找LastEvaluatedKey
. 只是想知道我们如何以有效的方式聚合所有项目?
app.get(path, function (req, res) {
var allItems = [];
var params = {
TableName: tableName,
"IndexName": "status-index",
"KeyConditionExpression": "#attrib_name = :attrib_value",
"ExpressionAttributeNames": { "#attrib_name": "status" },
"ExpressionAttributeValues": { ":attrib_value": req.query.status },
"ScanIndexForward": false
};
dynamodb.query(params, onQuery);
function onQuery(err, data) {
if (err) {
res.json({ error: 'Could not load items: ' + err });
} else {
// Should I be aggregating all the items like this?
allItems = allItems.concat(data.Items);
// Then should I set it to res like this to return all the items?
res.json(allItems);
if (typeof data.LastEvaluatedKey != 'undefined') {
params.ExclusiveStartKey = data.LastEvaluatedKey;
dynamodb.query(params, onQuery);
}
}
}
});
请查看代码中的注释。这就是我认为我们需要有适当的代码来聚合所有项目并返回响应的地方。
我还没有找到调试方法,因为我对 DynamoDB 和 AWS Amplify 还很陌生。让我知道是否有更简单的方法可以在 AWS 放大备份的 GET API 中进行调试。