Add an Update Note API

    ValidationException: ExpressionAttributeValues can only be specified when using expressions: FilterExpression and KeyConditionExpression are null
    at Request.extractError (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/protocol/json.js:52:27)
    at Request.callListeners (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/request.js:688:14)
    at Request.transition (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/request.js:690:12)
    at Request.callListeners (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
    at Request.emit (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/request.js:688:14)
    at Request.transition (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/request.js:690:12)
    at Request.callListeners (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
    at callNextListener (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/sequential_executor.js:96:12)
    at IncomingMessage.onEnd (/Users/aleksanderhayes/Downloads/notes-api/node_modules/aws-sdk/lib/event_listeners.js:313:13)
    at IncomingMessage.emit (events.js:327:22)
    at IncomingMessage.EventEmitter.emit (domain.js:467:12)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'ValidationException',
  time: 2021-01-13T10:25:08.652Z,
  requestId: '50B8LLSFMCP2MJF0VJ2049HUBNVV4KQNSO5AEMVJF66Q9ASUAAJG',
  statusCode: 400,
  retryable: false,
  retryDelay: 25.676944219402287
}
{
    "statusCode": 500,
    "body": "{\"error\":\"ExpressionAttributeValues can only be specified when using expressions: FilterExpression and KeyConditionExpression are null\"}"
}

I get this error.
I check amazon document at update item
It seems like the code is right. But the error tell me I should use FilterExpression or KeyConditionExpression, just not UpdateExpression.

Iā€™m getting the same error as you.

{
    "statusCode": 500,
    "body": "{\"error\":\"ExpressionAttributeValues can only be specified when using expressions: FilterExpression and KeyConditionExpression are null\"}"
}

Full update.js extract

import handler from "./libs/handler-lib";
import dynamoDb from "./libs/dynamodb-lib";

export const main = handler(async (event, context) => {
    const data = JSON.parse(event.body);
    const params = {
        TableName: process.env.tableName,
        // 'Key' defines the partition key and sort key of item to be updated
        Key: {
            userId: "123",
            noteId: event.pathParameters.id,
        },
        // 'UpdateExpression' defines attributes to be updated
        Expression: "SET content = :content, attachment = :attachment",
        // 'ExpressionAttributeValues' defines the value in the update expression
        ExpressionAttributeValues: {
            ":attachment": data.attachment || null,
            ":content": data.content || null,
        },
    };

    await dynamoDb.update(params);

    return { status: true };
});

EDIT: Turns out I had an error in my libs/dynamodb-lib.js file.

import AWS from "aws-sdk";

const client = new AWS.DynamoDB.DocumentClient();
    
export default {
    get: (params) => client.get(params).promise(),
    put: (params) => client.put(params).promise(),
    query: (params) => client.query(params).promise(),
    update: (params) => client.query(params).promise(), // THIS SHOULD BE client.update(params)
    delete: (params) => client.delete(params).promise(),
};
1 Like

Glad you figured it out.