0
  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?
3
  • Could you try using ` ExpressionAttributeValues: { ":a": "Connors" },` ? Commented Oct 14, 2021 at 18:41
  • ExpressionAttributeValues: { ":a": "Connors" },` did not return any records Commented Oct 14, 2021 at 19:24
  • Console logging the returned data would help. Also, consider using the DocumentClient going forward as it has better support for native JS type inference and you can replace constructs such as ":a": {S: "Connors"} with ":a": "Connors". Commented Oct 14, 2021 at 23:41

1 Answer 1

1

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...")
    }
);
Sign up to request clarification or add additional context in comments.

2 Comments

I have accepted Ahmad Nabil's answer. I was thinking my params were wrong. However my params were correct, the way I was processing the results was wrong. Because it is a query it returns a array of records. I corrected my code as follows: ddb.query(params).promise().then( function(data) { console.log("Customer:" + data); data.Items.forEach(function(customer) { customer.Fname + ": " + customer.Lname + customer.Dob + "\n"; }); return data.Items; }, function() { console.log("No records found...") } );
As jarmod mentioned I would also recommend using documentClient instead because it's easier to work with. Also consider using aws sdk v3 for better performance than sdk v2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.