繁体   English   中英

使用 DynamoDB 扫描重复项

[英]scan duplicates item with DynamoDB

我想扫描项目并避免使用重复代码。

所以,我正在尝试为它异步使用for-of

async function checkDupl(){
  const arr = new Array(10).fill(0);
  let code = '';
  for(const i of arr){
    //generate RANDOM CODE
    //for example, it would be '000001' to '000010'
    code = (Math.floor(Math.random() * 10) + 1).toString().padStart(6,"0");
    const params = { ... }; // it has filterExpression the code I generated randomly
    await DYNAMO_DB.scan(params, (err, res) => {
      if(res.Items.length === 0) {
        /* no duplicate! */
        return code;
      }
    }); 
  }
  return code;
}

console.log(checkDupl());
// it always return '';

我错过或误解了什么?

await只是等待 Promise (或thenable对象),但您正在使用带有“void” function 的await (您使用DYNAMO_DB.scan作为回调样式函数)。

我的建议,将DYNAMO_DB.scan与 Promise 样式一起使用( 方式

async function checkDupl() {
  const arr = new Array(10).fill(0);
  let code = '';
  for (const i of arr) {
    //generate RANDOM CODE
    //for example, it would be '000001' to '000010'
    code = (Math.floor(Math.random() * 10) + 1).toString().padStart(6, "0");
    const params = { ... }; // it has filterExpression the code I generated randomly

    const res = await DYNAMO_DB.scan(params).promise(); // convert to promise

    if (res.Items.length === 0) {
      /* no duplicate! */
      return code;
    }
    return code;
  }
}

(async () => {
  console.log(await checkDupl());
})();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM