hipsterstomper69
hipsterstomper69

Reputation: 355

Inserting a map item into DynamoDb

I have a table which is indexed by player accounts, and each player account has an empty map called "games." Whenever a new game is created, I'd like to insert a new item into that map for the new game, with some starting values. However this Javascript code below gives me an error:

            let newGame = {
                "live": { "BOOL": true },
                "result": { "S": "in_progress" }
            };

            let params = {
                TableName: "games_roster",
                Key: { "account_id" : playerAccount },
                UpdateExpression: "SET games.#game_id = :new_game",
                ExpressionAttributeNames: {
                    "#game_id": {"S": gameId}
                },
                ExpressionAttributeValues: {
                    ":new_game": {"M": newGame}
                }
            };

            await dynamoClient.send(new UpdateItemCommand(params));

The error:

TypeError: Cannot read properties of undefined (reading '0')
    at Object.AttributeValue.visit (models_0.js?31ed:996:1)
    at serializeAws_json1_0AttributeValue (Aws_json1_0.js?1450:4056:1)
    at eval (Aws_json1_0.js?1450:4484:1)
    at Array.reduce (<anonymous>)
    at serializeAws_json1_0Key (Aws_json1_0.js?1450:4478:1)
    at serializeAws_json1_0UpdateItemInput (Aws_json1_0.js?1450:5112:1)
    at eval (Aws_json1_0.js?1450:520:1)
    at step (tslib.es6.js?9ab4:102:1)
    at Object.eval [as next] (tslib.es6.js?9ab4:83:1)
    at eval (tslib.es6.js?9ab4:76:1)

I've verified that none of the variables are undefined by logging out the params object, so where would I be getting an TypeError?

Owing to @fedanov's answer, this is the correct format:

            let newGame = {
                live: { BOOL: true },
                result: { S: "in_progress" }
            };

            let params = {
                TableName: "games_roster",
                Key: { "account_id" : { S: playerAccount } },
                UpdateExpression: "SET games.#game_id = :new_game",
                ExpressionAttributeNames: {
                    "#game_id": gameId
                },
                ExpressionAttributeValues: {
                    ":new_game": { M: newGame }
                }
            };

            await dynamoClient.send(new UpdateItemCommand(params));

Upvotes: 1

Views: 1469

Answers (1)

fedonev
fedonev

Reputation: 25659

You are passing the wrong types to UpdateItemCommandInput. The Key parameter expects DynamoDB JSON values, ExpressionAttributeNames string values:

Key: { account_id: { S: playerAccount} },
ExpressionAttributeNames: { "#game_id" : gameId }

Upvotes: 1

Related Questions