Variable for Key in DynamoDB params?

Viewed 18

I have the following code. I was able to use a variable for the table name, and for the search param. But I don't seem to be able to get tableKey as a variable to work. When I run the code I get "Error Lambda: ValidationException: The provided key element does not match the schema". tableKey is set in this case to "PhoneNumber" which is the name of the field in my DDB table.

Is this possible to use a variable in this location?

async function handleRequest(searchParam,dbTable,tableKey) {
let Details = {
    TableName: dbTable,
    Key: {
      tableKey: searchParam,
    }
};
return docClient.get(Details).promise();
}
2 Answers

You cannot pass in the keys as a variable, only the values.

Actually, I was able to figure it out.

async function handleRequest(searchParam, dbTable, tableKey) {
    let Details = {
        TableName: dbTable,
        Key: {
        }
    };
    Details.Key[tableKey] = searchParam;
    return docClient.get(Details).promise();
Related