- I have a table called Customers with attributes CustId(partition Key), Fname, Lname, Dob.
- 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
}
}
}
]
}
- 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"
};
- Running the query
ddb.query(params).promise().then(
function(data) {
console.log("Customer:" data.Item)
return data.Item;
},
function() {
console.log("No records found...")
}
);
- I have a record with Lname = Connors.
- 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...")
}
);