我正在尝试从一个发电机流中建立一个流,以便它可以将数据放入另一个发电机表中,但是似乎没有将数据放入发电机目标表中
代码执行正常,cloudwatch不显示任何错误。它具有足够的权限来访问要尝试写入的发电机表。
var AWS = require("aws-sdk");
const dynamoDb = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event, context) => {
event.Records.forEach((record) => {
if (record.eventName != 'INSERT') {
console.log('Ignored ' + record.eventName + ' stream event.');
return;
}
var input = record.dynamodb.NewImage;
var id = input.pageId.S;
console.log(input);
console.log(id);
const params = {
TableName: "target-table",
Key: {
"targetId": id,
},
Item: {"targetId": id},
};
dynamoDb.put(params).promise();
console.log("put put the put put")
});
}
我希望将在一个表中输入的数据复制到目标中,但是似乎什么也没有发生。
答案 0 :(得分:0)
const dynamoDb = new AWS.DynamoDB.DocumentClient();
and
dynamoDb.put(params...
需要成为
const dynamodb = new AWS.DynamoDB();
and
dynamodb.putItem(params
我的lambda中使用的完整代码:
var AWS = require("aws-sdk");
exports.handler = async (event, context) => {
const dynamodb = new AWS.DynamoDB();
event.Records.forEach((record) => {
if (record.eventName != 'INSERT') {
console.log('Ignored ' + record.eventName + ' stream event.');
return;
}
var input = record.dynamodb.NewImage;
var id = input.pageId.S;
console.log(input);
console.log(id);
const params = {
TableName: "target-table",
Item: {"targetId": {S: "emailaddress"}},
};
dynamodb.putItem(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
console.log("put put the putput")
});
}
答案 1 :(得分:0)
您需要使用 await
关键字,因为您的处理程序函数是 async
函数
//(...)
await dynamoDb.put(params).promise();
// (...) return at outside of forEach loop
return {statusCode: 200, body : 'insertion is done'}