我有一个由玩家帐户索引的表,每个玩家帐户都有一个称为“游戏”的空地图每当创建一个新游戏时,我想在新游戏的地图中插入一个新项目,带有一些起始值。但是下面的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对象验证了所有变量都是未定义的,那么在哪里会出现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));