如何使用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:期望params.Item [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屋!