如何使用 AWS DynamoDB DocumentClient 将项目写入 DynamoDb? [英] How do I write an item to a DynamoDb with the AWS DynamoDB DocumentClient?
问题描述
我在使用 AWS DynamoDb JS SDK v2.4.9 时遇到问题.我想使用 DocumentClient 类而不是较低级别的 DynamoDb 类,但无法使其正常工作.
I'm having trouble with the AWS DynamoDb JS SDK v2.4.9. I want to use the DocumentClient class as opposed to the lower level DynamoDb class, but can't get it working.
这行得通:
function testPutItem( callback ) {
var tableName = 'todos';
var params = {
TableName: tableName,
Item: {
user_id: { S : userId },
id: { N : msFromEpoch }, // ms from epoch
title: { S : makeRandomStringWithLength(16) },
completed: { BOOL: false }
}
};
var dynamodb = new AWS.DynamoDB();
dynamodb.putItem(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
if (callback) callback(data);
}
});
}
这不起作用并给出错误 InvalidParameterType: Expected params.Item[attribute] to be a structure
for each attribute -- 好像 DocumentClient 期望与 DynamoDb 相同的输入:
This does not work and gives the error InvalidParameterType: Expected params.Item[attribute] to be a structure
for each attribute--as if DocumentClient is expecting the same input as DynamoDb:
function testPutItem( callback ) {
var tableName = 'todos';
var params = {
TableName: tableName,
Item: {
user_id: userId,
id: msFromEpoch,
title: makeRandomStringWithLength(16),
completed: false
}
};
console.log(params);
var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
docClient.put(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
if (callback) callback(data);
}
});
}
有人知道我做错了什么吗?
Does anyone have any idea what I am doing wrong?
推荐答案
我以前也有同样的问题,
I used to have the same issue,
请先尝试一个简单的对象,因为这是由于您的属性中的一些特殊字符,请参阅我的示例:
please try with a simple object first, cause it's due to some special characters in your attributes, see my example :
这会产生错误
InvalidParameterType:预期 params.Item[attribute] 是一个结构
InvalidParameterType: Expected params.Item[attribute] to be a structure
var Item = {
domain: "knewtone.com",
categorie: "<some HTML Object stuff>",
title: "<some HTML stuff>",
html: "<some HTML stuff>""
};
但是当我用格式化的 Html、简单字符替换 HTML 内容时,它可以工作
but when i replace the HTML stuff with a formated Html, simple characters , it works
var Item = {
domain: "knewtone.com",
categorie: $(categorie).html(),
title: $(title).html(),
html: $(html).html()
};
这篇关于如何使用 AWS DynamoDB DocumentClient 将项目写入 DynamoDb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!