我现在面临着返回的问题
Promise.all()
从内部
then()
块
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
const NUMBER_OF_ITEMS_PER_BATCH = 25;
const createBatches = (items) => {
// function that creates all the batches
}
function getPromisesArray (items) {
let batches = createBatches(items);
let promiseArr = [];
for(batch of batches) {
categoriesProductsBatchWriteParams.RequestItems[categoriesProductsTable] = batch;
promiseArr.push(dynamodb.batchWriteItem(categoriesProductsBatchWriteParams).promise)
}
return promiseArr;
}
// deleting an item from the first table
dynamodb.deleteItem(shopsCategoriesParams).promise()
// deleting an item from the second table
.then(() => dynamodb.deleteItem(categoriesTableParams).promise())
//querying the third table to get all the items that have a certain category id
.then(() => dynamodb.query(categoriesProductsQueryParams).promise())
.then(result => {
if(result.Count > NUMBER_OF_ITEMS_PER_BATCH) {
// deleting all those items with batchWrite
return Promise.all(getPromisesArray(result.Items));
} else {
categoriesProductsBatchWriteParams.RequestItems[categoriesProductsTable] = buildArrayForBatchWriteDelete(result.Items);
return dynamodb.batchWriteItem(categoriesProductsBatchWriteParams).promise()
}
})
.then(result => {
// this console logs [[Function: promise]]
console.log(result);
callback(null, 'categories were deleted successfully')
})
.catch(err => {
console.log(err);
callback(`Error: ${err}`);
})
我目前在dynamoDB中有三个不同的表,我必须按顺序从这些表中删除项目。一个表存储类别和商店之间的关系,一个表存储类别,最后,第三个表存储类别和产品之间的关系。因此,此函数的目的是删除一个类别。因此,我必须删除Shops\u Categories\u表中的关系(一对一),然后删除Categories表中的类别,最后删除第三个表中类别和产品之间的关系(一对多)。我正在使用categoryId分区键查询Categories\u Products\u表,以检索与该类别相关的所有产品。最后,我使用dynamodb batchwriteItem方法删除了所有产品。
所以这里的问题是,batchwriteItem一次最多只能删除25个项目,这就是为什么我要创建每个25个项目的批。这是通过createBatches函数完成的,该函数按预期工作。然后创建一个数组,其中存储
dynamodb.batchwrite(params).promise
承诺。我向
许诺全部()
方法并从then块中返回它。但不知何故,这些承诺没有得到执行,而是返回,然后返回控制台日志
[[Function: promise]]
。我尝试使用自己的自定义承诺,将其存储在一个数组中,并将其传递给
许诺全部()
效果很好。不管出于什么原因,尽管dynamodb的承诺没有得到执行。