首页 > 解决方案 > Lambda 函数在使用 Promise 调用 dynamoDB 后结束,但没有返回错误?

问题描述

我有一个 Lambda 函数使用 Promise 访问 DynamoDB。我没有收到任何错误,但该函数不会超出对 Dynamo 的调用。我发现的类似问题似乎可以通过使用 Promise 解决,所以我觉得这是一个新问题。

我尝试使用带有 then 语句的 promise 来等待 DynamoDB 返回数据。我没有收到任何错误,但我的最后一条日志语句就在调用 dynamo 之前。

这是我的发电机辅助功能:

let AWS = require("aws-sdk");
let dynamo = new AWS.DynamoDB.DocumentClient({
  region: 'us-east-1'
});
//let dynamo = new AWS.DynamoDB() //This may get rid of the execption but gotta make it work. Think I have to wrap the data witht the types in dynamo
//let dynamoPromise = dynamo.promise()
let dynamoHelper = function() {
  return {
    putObject: function(tableName, object) { //,callback I took callback out of the params
      let params = {
        "TableName": tableName,
        "Item": object
      };

      let dynamoPromise = dynamo.put(params).promise()

      return dynamoPromise;
    },
    getRoom: function(tableName, deviceId) { //,callback I took callback out of the params
      console.log("inside the dynamoHelper.getRoom function")
      let params = {
        "TableName": tableName,
        "Key": {
          "deviceId": deviceId
        },
        "AttributesToGet": [
          "roomNumber"
        ]
      };
      let dynamoPromise = dynamo.get(params).promise() //? 
      console.log("After the get call to dynamo still inside dynamo helper")
      return dynamoPromise;
    }
  }
}();
module.exports = dynamoHelper;

这是我在 index.js 中调用它的地方:

let dynamoGetPromise = dynamoHelper.getRoom(config.room_lookup_dynamodb_table, deviceId) //took the callback out of the args and put it in then. Will have to do the same for the one that is contained inside the then.
    dynamoGetPromise
      .then(
        function(data) {
          console.log("Data: " + JSON.stringify(data));
          if ('Item' in data) {
            roomNumber = data.Item.roomNumber;
          } else {
            roomNumber = 9999;
          }

...........

console.log("Data: " + JSON.stringify(data));声明从未达到,但 - 再次 - 没有错误。不应该是超时问题,因为我的 Lambda 输出显示计费持续时间仅为 700 毫秒...

这是我的 lambda 输出:

START RequestId: e57c0e7a-bdf2-4e20-869b-44babf834dfd Version: $LATEST 2019-04-04T14:30:52.075Z e57c0e7a-bdf2-4e20-869b-44babf834dfd Warning: Application ID is not set 2019-04-04T14:30:52.193Z e57c0e7a-bdf2-4e20-869b-44babf834dfd { canHandle: [Function: canHandle], handle: [Function: handle] } 2019-04-04T14:30:52.254Z e57c0e7a-bdf2-4e20-869b-44babf834dfd ||| New Session ||| 2019-04-04T14:30:52.254Z e57c0e7a-bdf2-4e20-869b-44babf834dfd Device ID: amzn1.ask.device.AEH2LHYGV7GSPP5THMR5H56AI2OOMAQ7MF54CZ3E6WR433WGS6QAOCYCKJWRJ3TQY5IE76NWR2IKCANB6TJNKLDEZOO2YN6ACUVT33MKSS4CO6R7GJI6GDFLOBOPUA2IXX7RI732UXJ6PDST5KYC7CSQK634K4APEBRNVOKVZIDECOCBBIFB4 2019-04-04T14:30:52.254Z e57c0e7a-bdf2-4e20-869b-44babf834dfd New Intent Time: 2019-04-04T10:30:52-04:00 2019-04-04T14:30:52.254Z e57c0e7a-bdf2-4e20-869b-44babf834dfd inside the dynamoHelper.getRoom function END RequestId: e57c0e7a-bdf2-4e20-869b-44babf834dfd REPORT RequestId: e57c0e7a-bdf2-4e20-869b-44babf834dfd Duration: 693.31 ms Billed Duration: 700 ms Memory Size: 128 MB Max Memory Used: 74 MB

谢谢你的帮助!

更新:

直接在 dynamo.get 调用中添加了一条 catch 语句,但仍然没有更多信息:

let dynamoPromise = dynamo.get(params).promise().catch(function(reason){console.log("Reason: " + reason)});

更新2:

尝试摆脱 IIFE,并将它们变成对象中的异步函数。结果相同。

在下面尝试了@alex067 的建议,但结果相同。

更新3:

我已经使用 async/await 解决了大约 90% 的问题。

标签: node.jsamazon-web-servicespromiseaws-lambdaamazon-dynamodb

解决方案


推荐阅读