GameSparks().DynamoDB().QueryIndex example please

0

Can someone provide a working example of using QueryIndex in GameSparks cloud code for handler in an Event. I'm attempting to return a list of players in a lobby querying my table named BattleRoyalePlayerLobbyTable, with lobbyId, playerId as complex key, and index named lobbyId-index with primary key as lobbyId, using message.lobbyId passed in the request message.

try
{
  const tableName = "BattleRoyalePlayerLobbyTable";
  const indexName = 'lobbyId-index';
  const keyConditionExpression = "lobbyId = :lobbyid";
  const expressionAttributeNames = null;
  const expressionAttributeValues = { ":lobbyid": {"S": message.lobbyId} };
  const exclusiveStartKey = null;
  
  let queryResult = GameSparks().DynamoDB().QueryIndex(
    tableName,
    indexName,
    keyConditionExpression,
    expressionAttributeNames,
    expressionAttributeValues,
    exclusiveStartKey);
  
  GameSparks().Logging().Info(queryResult.ToString());
  
  return GameSparks().Messaging.Response({ "players": queryResult.Items });
}
catch (ex)
{
  return GameSparks().Messaging().Response({ "players": [], "error": ex.message });
}
Response received
Id: 56f05f7b-65ed-43b7-a8e4-8390dc9faf0e
 Type: Custom.Game.GetPlayersInLobby
 Category: Response
 ResponseTo: 92f4562c-cf9e-4f7a-ac13-444e31b1c93b
 
 {"players":[],"error":"javaException.getCustomerFacingExceptionDetails is not a function"}
Request sent
Id: 92f4562c-cf9e-4f7a-ac13-444e31b1c93b
 Type: Custom.Game.GetPlayersInLobby
 Category: Request
 
 {"lobbyId":"fb3578b6797848448d5facca5555d12c"}

Thank you

asked 2 years ago316 views
1 Answer
0
Accepted Answer

I figured it out. Attribute Values are type MAP.

try
{
  const tableName = 'BattleRoyalePlayerLobbyTable';
  const indexName = 'lobbyId-index';
  const keyConditionExpression = '#lobbyId = :lobbyid';
  const expressionAttributeNames = {'#lobbyId': 'lobbyId'};
  const expressionAttributeValues = { ':lobbyid': message.lobbyId };
  const exclusiveStartKey = {};
  
  let queryResult = GameSparks().DynamoDB().QueryIndex(
    tableName,
    indexName,
    keyConditionExpression,
    expressionAttributeNames,
    expressionAttributeValues,
    exclusiveStartKey);
  
  return GameSparks().Messaging().Response({ 'players': queryResult.Items });
}
catch (ex)
{
  return GameSparks().Messaging().Response({ "players": [], "error": ex.message });
}
answered 2 years ago
  • @Kirkules hi can you help me? I made a query just like yours but its gives me this error "Content does not match constraints applied by annotation [PatternConstraint]"

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions