gpt4 book ai didi

node.js - Node lambda 未写入 DynamoDB 且没有错误

转载 作者:行者123 更新时间:2023-12-01 13:15:55 25 4
gpt4 key购买 nike

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"

最佳答案

'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 的类型。这是 问题在你的代码上。只需指定原始值(就像我上面所做的那样,传递字符串本身),它就会起作用。

关于node.js - Node lambda 未写入 DynamoDB 且没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066777/

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