question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: SyntaxError: Unexpected token What is wrong?

I get an error message in my cloud script code but I don't know what is wrong.

SyntaxError: Unexpected token > at Script Document [3]:14:105 -> server.UpdateUserReadOnlyData({PlayFabID: currentPlayerId, Data: new Dictionary<string, string>() {
                {"Country", NewCountry}}, Permission: UserDataPermission.Public});

What is wrong with my cloudScript?

My cloudScript:

handlers.UpdateCountryReadOnlyData = function (args, context)
{
   var NewCountry = args.PlayerDataCountry;
   var resultdata = server.GetUserData({PlayFabID: currentPlayerId, Keys: null})
   var CurrentCountry = "";
   
   if (resultdata.Error == null)
   {
      if ((resultdata.Result.Data != null) && (resultprofile.Result.Data.ContainsKey("Country")))
      {
        CurrentCountry = resultprofile.Result.Data["Country"].Value;
    
        if ((CurrentCountry == null) || (CurrentCountry == ""))
          server.UpdateUserReadOnlyData({PlayFabID: currentPlayerId, Data: new Dictionary<string, string>() {
                {"Country", NewCountry}}, Permission: UserDataPermission.Public});
      }
      else
        server.UpdateUserReadOnlyData({PlayFabID: currentPlayerId, Data: new Dictionary<string, string>() {
                {"Country", NewCountry}}, Permission: UserDataPermission.Public});
   }
}

Client code:

 private async Task UpdateCountry()
 {
     var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
     {
         FunctionName = "UpdateCountryReadOnlyData",
         FunctionParameter = new { PlayerDataCountry = "Germany" },
         GeneratePlayStreamEvent = true
     });

     if (result.Error != null)
         Console.WriteLine(result.Error.Error.ToString());
     else
         Console.WriteLine("Country: " + "Germany");
 }
CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

Hi Kim,

I found several syntax errors in your CloudScript code, please note that CloudScript is based on Javascript, don’t mistake it with C# syntax. For the error message you posted in the begainning, there is no syntax in JavaScript like “Data: new Dictionary<string, string>() { {"Country", NewCountry}}”. Please check our typescript definition repro, it will be easy to avoid syntax and type issues. Anyway, I re-wrote the CloudScript you posted and tested in my title for your reference, you can make some changes on it to fit your own requirements.

handlers.UpdateCountryReadOnlyData = function (args, context)
{
   var NewCountry = args.PlayerDataCountry; 
   var resultdata = server.GetUserReadOnlyData({PlayFabId: currentPlayerId, Keys: "Country"}); // first search for the current country info
   var CurrentCountry = ""; 
   
   if(resultdata.Data.hasOwnProperty("Country")){ // if country info already exists in readonly data 
       
          if ((resultdata.Data.Country.Value != null) && (resultdata.Data.Country.Value != "")){ // make sure the value is not null or ""
   
                CurrentCountry = resultdata.Data.Country.Value;
                log.info("Already has country info, current country is:" + CurrentCountry.toString()); // if country value already exists, notify the client that Country info already been set
            }
          else{ // if country value is null or "", set the Country value
              server.UpdateUserReadOnlyData({
           PlayFabId: currentPlayerId,
           Data: {
               "Country": NewCountry
           }
           });
          }
   }
   else{// if country  hasn't been set yet, set it for the user
       server.UpdateUserReadOnlyData({
           PlayFabId: currentPlayerId,
           Data: {
               "Country": NewCountry
           }
           });
   }
   return server.GetUserReadOnlyData({PlayFabID: currentPlayerId, Keys: "Country"}); // return the current country info to the clients.
}
<br>
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kim Strasser avatar image Kim Strasser commented ·

Thank you. It works.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.