将地图项插入 DynamoDb

Inserting a map item into DynamoDb

我有一个 table 由玩家帐户索引,每个玩家帐户都有一个名为“游戏”的空地图。每当创建新游戏时,我都想在新游戏的地图中插入一个新项目,并带有一些起始值。但是下面的 Javascript 代码给我一个错误:

            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));

错误:

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)

我已经通过注销 params 对象验证了 none 个变量未定义,那么我会在哪里收到 TypeError?

由于@fedanov 的回答,这是正确的格式:

            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));

您将错误的类型传递给 UpdateItemCommandInputKey 参数需要 DynamoDB JSON 值,ExpressionAttributeNames 字符串值:

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