所以我试着编写一个获取params的函数并调用DynamoDB的scan方法,该方法返回分页结果,并且我将limit
作为输入从params开始,所以我不想递归调用我的扫描函数来获取所有数据,而是希望获得尽可能多的limit
数据作为参数。我尝试了以下代码块,但似乎我的逻辑不正确!你能帮我解决一下我做错的事吗?
function scan(params, total, callback) {
dynamo.scan(params, function(err, data) {
if (err) {
callback(err, data);
} else {
if (((!params.Limit || (params.Limit && total.length < params.Limit)))
&& data.Items && data.LastEvaluatedKey) {
params.ExclusiveStartKey = data.LastEvaluatedKey;
(function(data) {
scan(params, data.Items.concat(data.Items), function(err, result) {
data.Items = data.Items.concat(result.Items);
if (result.LastEvaluatedKey)
data.LastEvaluatedKey = result.LastEvaluatedKey;
callback(err, data);
});
} (data));
} else {
callback(err, total); //Here it says undefined is not a function
}
}
});
}
通过以下代码致电:
//params is stuffed with needed parameters for scan method of DynamoDB
scan(params,[],function(err, data){
//Processing data here
})
答案 0 :(得分:0)
以下是我编写的一些示例代码,请告诉我它是否适合您的用例,或者您是否有任何问题。
/**
* This method will scan continue to scan until
* 1) There are no more items (LastEvaluatedKey is null)
* 2) We have retrieved N items from paginated scan
* If we find more than N, we take the first N items.
**/
function scanWithItemLimit(params, n, callback, items) {
db.scan(params, function(err, data) {
if (err) {
callback(err);
} else {
var leftover = n - data.Items.length;
var itemsCopy = items ? items : [];
if (leftover > 0) {
itemsCopy = itemsCopy.concat(data.Items);
var paramsCopy = params;
paramsCopy.ExclusiveStartKey = data.LastEvaluatedKey;
if (params.ExclusiveStartKey) {
scanWithItemLimit(paramsCopy, leftover, callback, itemsCopy);
} else {
callback(err, itemsCopy);
}
} else {
if (n != 0) {
for (var i = 0; i < n; i++) {
itemsCopy.push(data.Items[i]);
}
}
callback(err, itemsCopy);
}
}
});
}
var pfunc = function(err, items) { console.log(err, items); };
params = {TableName: "Table"};
scanWithItemLimit(params, 5, pfunc);
&#13;