gpt4 book ai didi

amazon-web-services - Amazon Lambda 写入 DynamoDB

转载 作者:行者123 更新时间:2023-12-04 16:04:26 26 4
gpt4 key购买 nike

我是 dynamoDB 的新手,我正在尝试使用 Lambda 将一些数据写入表中。到目前为止我有这个:

'AddFood': function () {

var FoodName = this.event.request.intent.slots.FoodName.value;
var FoodCalories = this.event.request.intent.slots.FoodCalories.value;
console.log('FoodName : ' + FoodName);

const params = {
TableName: 'Foods',
Item: {
'id': {"S": 3},
'calories': {"S": FoodCalories},
'food': {"S": FoodName}
}
};

writeDynamoItem(params, myResult=>{
var say = '';

say = myResult;

say = FoodName + ' with ' + FoodCalories + ' calories has been added ';
this.response.speak(say).listen('try again');
this.emit(':responseReady');

});

function writeDynamoItem(params, callback) {

var AWS = require('aws-sdk');
AWS.config.update({region: AWSregion});

var docClient = new AWS.DynamoDB();

console.log('writing item to DynamoDB table');

docClient.putItem(params, function (err, data) {
if (err) {
callback(err, null)
} else {
callback(null, data)
}
});
}
}

有谁知道为什么数据没有出现在数据库中?我已经检查了 IAM,并且策略设置为 AmazonDynamoDBFullAccess。

最佳答案

对写入函数进行一些更改后,以下代码允许我将项目写入数据库:

函数 writeDynamoItem(参数,回调){

const AWS = require('aws-sdk');
AWS.config.update({region: AWSregion});

const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});

console.log('writing item to DynamoDB table');

docClient.put(params, function (err, data) {
if (err) {
callback(err, null)
console.error("Unable to write item. Error JSON:", JSON.stringify(err, null, 2))
} else {
callback(null, data)
}
});

关于amazon-web-services - Amazon Lambda 写入 DynamoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49349216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com