我有一个Lambda函数,可以利用Promise连接到DynamoDB。我没有收到任何错误,但该功能并没有超出对Dynamo的调用。我发现类似的问题似乎可以通过使用诺言来解决,所以我觉得这是一个新问题。
我尝试过将promise与then语句一起使用,以等待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输出显示计费持续时间仅为700ms ...
这是我的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)});
Update2:
试图摆脱IIFE,并将它们转变为保存在对象中的异步函数。结果相同。
尝试了以下@ alex067的建议,但结果相同。
Update3:
我已经使用async / await解决了大约90%的问题。
答案 0 :(得分:0)
我使用了异步并等待,这解决了函数无法完全执行的问题。我仍然有一个问题,但是如有必要,我将为此发布一个新问题。