【发布时间】:2016-12-22 01:30:31
【问题描述】:
我完全是 node.js 的菜鸟。我正在尝试向 AWS dynamoDB 表添加一个项目,并将一个字符串返回给 Twilio。我认为我的 Lambda 函数在编写 dynamoDB 条目之前就结束了。我得到正确返回到 Twilio 的字符串,但数据库中没有项目。我确实在 Cloudwatch 日志中看到了字符串“putItem Function Called”。
var AWS = require('aws-sdk');
var dynamoDBConfiguration = {"region": "us-east-2"};
AWS.config.update(dynamoDBConfiguration);
var dd = new AWS.DynamoDB();
var tableName = 'kta';
console.log('Loading function');
exports.handler = function(event, context) {
//*****************************************
try {
putItem = function(param,result) {
console.log(" putItem Function Called");
var d = new Date();
var last_updated = d.getUTCFullYear() + "/" + d.getUTCMonth() + "/" + d.getUTCDate() + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds() + "." + d.getUTCMilliseconds() + " UTC";
var item = {
'param': { 'S': 'inboundSmsLastNumber' },
'result': { 'S': '+18885551212'},
'last_updated': { 'S': last_updated }
};
console.log("Data: %j",item);
var response = dd.putItem({
'TableName': tableName,
'Item': item
}, function(err, data) {
if (err) {
context.fail("Error in putItem "+err);
} else {
context.succeed("Successfully Inserted");
}
});
};
putItem();
} catch (error) {
context.fail("Caught: " + error);
}
//*****************************************
qryObject = parseQuery(event.reqbody);
console.log(qryObject);
// Send SMS with the inbound SMS information
context.succeed("<Sms from='+18885551212' to='+19991112222'>You received an SMS from " + qryObject.From + "</Sms>");
};
function parseQuery(qstr) {
var query = {};
var a = qstr.substr(0).split('&');
for (var i = 0; i < a.length; i++) {
var b = a[i].split('=');
query[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
}
return query;
}
非常感谢任何帮助。
这里是 Cloudwatch 日志:
2016-12-22T01:15:07.503Z 未定义加载函数
START RequestId:134fda9f-c7e4-11e6-80bc-25be850f5913 版本:$LATEST
2016-12-22T01:15:07.522Z 134fda9f-c7e4-11e6-80bc-25be850f5913 putItem 函数调用
2016-12-22T01:15:07.522Z 134fda9f-c7e4-11e6-80bc-25be850f5913 数据:
{ “参数”:{ "S": "inboundSmsLastNumber" }, “结果”: { “S”:“+18885551212” }, “最近更新时间”: { “S”:“2016/11/22 1:15:7.522 UTC” } }
2016-12-22T01:15:08.123Z 134fda9f-c7e4-11e6-80bc-25be850f5913 { ToCountry: 'US', ToState: '', SmsMessageSid: 'SM15398089fc1dbaeedb56e560face6380', NumMedia: '0', ToC ,FromZip:'66610',SmsSid:'SM15398089fc1dbaeedb56e560face6380',FromState:'KS',FromCity:'TOPEKA',正文:'Test+65',FromCountry:'US',到:'+1****** ****',ToZip:'',NumSegments:'1',MessageSid:'SM15398089fc1dbeedb56e560face6380',AccountSid:'AC**************************** *********',来自:'+1**********',ApiVersion:'2010-04-01' }
END 请求 ID:134fda9f-c7e4-11e6-80bc-25be850f5913
报告请求 ID:134fda9f-c7e4-11e6-80bc-25be850f5913 持续时间:838.90 毫秒 计费持续时间:900 毫秒内存大小:128 MB 使用的最大内存:21 MB
【问题讨论】:
-
你能打印你的 lambda 函数的日志输出吗?
-
您的 Cloudwatch 日志在每次执行的最后一行显示什么?它应该说类似
REPORT RequestId: ef2b5301-c7e9-11e6-99a1-f53533ec880e Duration: 192.71 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 18 MB No newer events found at the moment. Retry.
标签: node.js amazon-web-services lambda twilio