var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'})
AWS.config.update({region: 'eu-west-1'})
var docClient = new AWS.DynamoDB.DocumentClient()
const params = {
TableName: 'TC_QUESTIONS',
Item: {
'questionId' : {S: '001'},
'questionText' : {S: 'Richard Roe'}
}
}
var putItemPromise = docClient.put(params).promise()
putItemPromise.then(function(data) {
console.log("Added item:", JSON.stringify(data, null, 2));
}).catch(function(err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
});
await putItemPromise
没有错误返回。没有成功的回调。当我使用错误的列名时,我从Dynamo收到错误消息。我尝试在本地和lambda上运行它。它执行并退出。没有行被添加到表中。我在做什么错了?
编辑: 这是我用于DynamoDB表的CF:
QuestionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: 'TC_QUESTIONS'
AttributeDefinitions:
- AttributeName: questionId
AttributeType: S
- AttributeName: questionText
AttributeType: S
KeySchema:
- AttributeName: questionId
KeyType: HASH
- AttributeName: questionText
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
答案 0 :(得分:1)
'use strict';
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});
exports.handler = async (event) => {
const params = {
TableName: 'TC_QUESTIONS',
Item: {
'questionId': '001',
'questionText' : 'Richard Roe'
}
}
try {
await docClient.put(params).promise()
} catch (e) {
console.log(e.message)
}
};
如果您的表存在并且questionId是您的分区键,则上面的代码有效。
我认为您有点混为一谈,因为自从您使用DocClient以来,您不再需要指定DynamoDB的类型。这就是您代码中的问题。只需指定原始值(就像我上面所做的那样,传递String本身),它将起作用。