Folks, New to Javascript... trying to do simple dynamo queries from node:
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
var db = new AWS.DynamoDB();
var params = {
"TableName" : 'admins',
"Key" : [
{ "username" : { "S" : "foo" } },
],
}
db.getItem(params, function(err, data) {
console.log('error: '+ err);
console.log(data);
return next();
res.send(data);
});
}
Output:
error: UnexpectedParameter: Unexpected key 'username' found in params.Key['0']
Thanks! Any help would be greatly appreciated!
The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item, GetItem does not return any data and there will be no Item element in the response. GetItem provides an eventually consistent read by default.
To access DynamoDB, create an AWS. DynamoDB service object. To identify the item to get, you must provide the value of the primary key for that item in the table. By default, the getItem method returns all the attribute values defined for the item.
There is no performance difference between the two. The hash calculation in both the queries are done 1 by 1. The latter, i.e., get item is just provided as an analogy to the JPA repository/spring findOne/findById to make wiring in Spring Bean wiring/ Hibernate configs easier.
Must follow the SDK and Docs, its simple: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html
var params = {
AttributesToGet: [
"password"
],
TableName : 'foo',
Key : {
"username" : {
"S" : "bar"
}
}
}
db.getItem(params, function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
console.log(data); // successful response
res.send(data);
}
return next();
});
I was trying to do it as it was suggested in the documentation, but also got errors.
At the end the following worked:
var aws = require('aws-sdk');
var db = new aws.DynamoDB({
region: 'eu-central-1',
maxRetries: 1
});
exports.handler = event => {
return queryMyThings();
}
const queryMyThings = async (event) => {
var params = {
Key: {
"ColumnByWhichYouSearch": {
S: "ValueThatYouAreQueriing"
}
},
TableName: "YourTableName"
};
return await db.getItem(params).promise();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With