Home > OS >  Lambda - Querying DynamoDB through Lambda
Lambda - Querying DynamoDB through Lambda

Time:10-15

  1. I have a table called Customers with attributes CustId(partition Key), Fname, Lname, Dob.
  2. I created a secondary index called LastNameIndex on Lname with the following params:
{
    TableName: 'Customers'
    AttributeDefinitions: [
        {
          AttributeName: 'Lname',
          AttributeType: 'S'
        }
    ],
    GlobalSecondaryIndexUpdates: [

        {
            Create: {
                IndexName: "LastNameIndex",
                KeySchema: [
                    {AttributeName: "Lname", KeyType: "HASH"}
                    
                ],
                Projection: {
                    "ProjectionType": "ALL"
                },
                ProvisionedThroughput: {                                
                    "ReadCapacityUnits": 1,"WriteCapacityUnits": 1
                }
            }
        }
    ]
}
  1. Lambda function (snippet) - I want to get all records with Lname=Connors
params = {         
    TableName: "Customers",
    IndexName: "LastNameIndex",
    ExpressionAttributeNames: {
        "#FN": "Fname", 
        "#LN": "Lname",
        "#DB": "Dob",
        
    }, 
    ExpressionAttributeValues: {
        ":a": {
            S: "Connors"
        } 
    }, 
    KeyConditionExpression: "Lname = :a",
    ProjectionExpression: "#FN, #LN, #DB"
};
  1. Running the query
ddb.query(params).promise().then(

    function(data) {
        console.log("Customer:"   data.Item)
        return data.Item;
    },
    function() {
        console.log("No records found...")
    }
);
  1. I have a record with Lname = Connors.
  2. But the query does not return me any records - any idea what's wrong with the params?

CodePudding user response:

The query operation returns multiple items not a single item like getItem operation. so I think returning data.Items instead of data.Item should work just fine.

ddb.query(params).promise().then(

    function(data) {
        console.log("Customer:"   data.Items)
        return data.Items;
    },
    function() {
        console.log("No records found...")
    }
);
  • Related