0

I want to make a query to return all entries with a certain userID, in this case Will666. I have a primaryKey and a sortKey.

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'eu-west-2', apiVersion: '2012-08-10'});

exports.handler =  (event, context, callback) => {


    const params = {

        TableName: "signalepisodes",
        KeyConditionExpression: "userID = :a",
        ExpressionAttributeValues: {
        ":a": "Will666"
    }

    };
    dynamodb.query(params, function(err, data){
          if (err) {
            console.log(err);
            callback(err);
        } else {
            console.log(data);
              const items = data.Items.map(
                (dataField) => {
                  return { userID: dataField.userID.S, uploadDate: dataField.uploadDate.N, epTitle: dataField.epTitle.S };
              } 

                );

            callback(null, items);

        }
    });
    };

i get this error message when i test it. I guess my syntax is wrong but i can't work it out.

"errorType": "MultipleValidationErrors",

my dynamoDB table looks like this:

enter image description here

1 Answer 1

1

The DynamoDB SDK has two types of client:

  1. low-level client: new AWS.DynamoDB(...)
  2. high-level client: new AWS.DynamoDB.DocumentClient(...)

You are currently using #1, but you are supplying attributes to your query as if you were using the document client #2.

So, either switch to the DocumentClient, and continue to use:

{":a": "Will666"}

Or stick with the low-level client and change your attributes to indicate value types, for example:

{":a": {"S": "Will666"}}

I'd recommend the DocumentClient because it significantly simplifies the marshalling and unmarshalling of data.

I would also recommend updating your code from the old callback style asynchronous code to the newer Promise-based options. For example, something like this:

exports.handler = async (event, context) => {
    const params = {
        TableName: "signalepisodes",
        KeyConditionExpression: "userID = :a",
        ExpressionAttributeValues: { ":a": "Will666" }
    };

    const items = await dynamodb.query(params).promise();

    for (const item of items) {
        console.log('Item:', item);
    }

    return items;
}
Sign up to request clarification or add additional context in comments.

Comments

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.