I am using the javascript resolver to update an item on the database but am getting this error:
The provided key element does not match the schema.
Resolver request:
export function request(ctx) {
const { username } = ctx.arguments.input;
const { identity } = ctx;
const now = util.time.nowISO8601();
const user = `USER#${identity.claims.sub}`
return {
operation: "UpdateItem",
key: util.dynamodb.toMapValues({ pk: user }),
update: {
expression: 'SET #username = :username',
expressionNames: { '#username': 'username' },
expressionValues: { ':username': { S: username } },
}
}
Request function evaluation from AppSync cloudwatch:
{
"logType": "RequestFunctionEvaluation",
"fieldName": "updateUsername",
"resolverArn": "arn:aws:appsync:us-xxxx-2:xxxxx:apis/xxxxx/types/Mutation/resolvers/updateUsername",
"functionName": "update_username_function",
"fieldInError": false,
"evaluationResult": {
"operation": "UpdateItem",
"key": {
"pk": {
"S": "USER#118b6520-80c1-7074-0ed5-616bf57fbc3b"
}
},
"update": {
"expression": "SET #username = :username",
"expressionNames": {
"#username": "username"
},
"expressionValues": {
":username": {
"S": "dfgdf"
}
}
}
},
"parentType": "Mutation",
"path": [
"updateUsername"
],
"requestId": "172905a5-a810-400d-9e65-55bdc8a23964",
"context": {
"arguments": {
"input": {
"username": "dfgdf"
}
},
"prev": {
"result": []
},
"stash": {},
"outErrors": []
},
"errors": [],
"graphQLAPIId": "ivxudb2wxfda7htbuk64eje66e",
"functionArn": "arn:aws:appsync:us-east-2:637453376396:apis/xxxxxx/functions/xxxxxx"
}
Response evaluation:
...
"error": {
"message": "The provided key element does not match the schema (Service: DynamoDb, Status Code: 400, Request ID: L7GLI66HRS7H7GPA4TN3RT2F1FVV4KQNSO5AEMVJF66Q9ASUAAJG)",
"type": "DynamoDB:DynamoDbException"
},
...
Following docs here: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-resolvers-js.html
import { util } from '@aws-appsync/utils';
export function request(ctx) {
const { id, direction } = ctx.arguments;
const field = direction === 'UP' ? 'ups' : 'downs';
return {
operation: 'UpdateItem',
key: util.dynamodb.toMapValues({ id }),
update: {
expression: `ADD ${field} :plusOne, version :plusOne`,
expressionValues: util.dynamodb.toMapValues({ ':plusOne': 1 }),
},
};
}
export function response(ctx) {
return ctx.result;
}
What would be causing this to throw this error? I have confirmed that the pk in my database is of name "pk" and type string.
Figured out that my issue was a missing sk in my key. I thought the sk was optional, but that's only in the case where you may not have an sk.
So it becomes:
key: util.dynamodb.toMapValues({pk: user, sk: "CONFIG"})