问题
The following is node.js query in AWS lambda on a dynamoDB JSON object. UserID is the primary key with no sort key. GeoHash is a secondary key, with index name "GeoHash-index". The call succeeds with no error, but does not result in anything being returned. It's possible that the test data below is wrong as it does not offer any connection to the index name, but I'm new to AWS/noSQL and a little lost.
var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = function(event,context,callback) {
console.log(JSON.stringify(event, null, ' '));
var tableName = "table1";
// getItem
docClient.getItem({
TableName: tableName,
IndexName: "GeoHash-index",
KeyConditionExpression: "GeoHash = :geohash",
ExpressionAttributeValues: {":geohash": "dpz886gb0tb0"}
}),
function(err,data){
if(err){
callback(err);
} else {
callback(null,data);
}
}
};
Where the lambda test data is
{
"GeoHash": "dpz886gb0tb0",
"Radius": 2,
"Timestamp": 1472601493180,
"UserID": "User1"
}
The GeoHash string should match each other. Thoughts?
EDIT No success with this method either
var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = function index(event, context, callback) {
var params = {
TableName: "LocationAware1",
IndexName: "GeoHash-index",
KeyConditionExpression: "GeoHash = :geohash",
ExpressionAttributeValues: {
":geohash": {'S': 'dpz886gb0tb0'}
},
};
docClient.query(params, function(err, data) {
if (err)
console.log(JSON.stringify(err));
else
console.log(JSON.stringify(data));
});
}
回答1:
var AWS = require('aws-sdk');
exports.handler = function(event,context,callback) {
var params = {
TableName: 'Table1',
IndexName: 'GeoHash-index',
KeyConditionExpression: 'GeoHash = :geohash',
ExpressionAttributeValues: {
':geohash': 'dpz886g8p9e2',
}
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.query(params, function(err, data) {
if (err) callback(err);
else callback(null, data);
});
}
Rewrote and it's clean.
回答2:
When you say the call succeeds with no error i am assuming you mean the call to the lambda
If you are using node version 4.3 the way you are returning from the function is deprecated as seen from this extract from the aws lambda documentation
The Node.js runtime v4.3 supports the optional callback parameter. You >can use it to explicitly return information back to the caller. The general syntax is:
callback(Error error, Object result);
Using the callback parameter is optional. If you don't use the optional callback parameter, the behavior is same as if you called the callback() without any parameters. You can specify the callback in your code to return information to the caller. If you don't use callback in your code, AWS Lambda will call it implicitly and the return value is null.
This is the correct way to return from an aws lambda function if using node version 4.3
first add a 3rd parameter to the handler function like so
exports.handler = function(event,context,callback)
then when returning from the function follow this form
function(err,data){
if(err){
callback(err);
} else {
callback(null,data);
}
}
来源:https://stackoverflow.com/questions/39239035/aws-lambda-querying-secondary-index