question

Kim Strasser avatar image
Kim Strasser asked

Title display name changed to "undefined"?

Is it normal that the title display name changes to "undefined" if creating/updating the display name fails in CloudScript?

I had a JavascriptException in my CloudScript and after that the title display name changed to undefined.

Will the title display name always change to undefined if something goes wrong with creating/updating the title display name?

Raw event JSON
{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "VerifyText",
    "CloudScriptExecutionResult": {
        "FunctionName": "VerifyText",
        "Revision": 62,
        "FunctionResult": null,
        "FunctionResultTooLarge": null,
        "Logs": [
            {
                "Level": "Info",
                "Message": "Display name changed",
                "Data": null
            }
        ],
        "LogsTooLarge": null,
        "ExecutionTimeSeconds": 0.10228119999999999,
        "ProcessorTimeSeconds": 0,
        "MemoryConsumedBytes": 73296,
        "APIRequestsIssued": 1,
        "HttpRequestsIssued": 1,
        "Error": {
            "Error": "JavascriptException",
            "Message": "JavascriptException",
            "StackTrace": "TypeError: Cannot read property 'toString' of undefined\n    at Object.handlers.UpdateDisplayname (BFD0A-main.js:109:36)\n    at handlers.VerifyText (BFD0A-main.js:176:18)"
        }
    },
    "EventNamespace": "com.playfab",
    "EntityType": "player",
    "TitleId": "BFD0A",
    "EntityId": "1066CF1F71C39EC1",
    "EventId": "095c9614065b4385bbbc3b235a14f2a5",
    "SourceType": "BackEnd",
    "Timestamp": "2019-10-10T08:22:51.0393116Z",
    "History": null,
    "CustomTags": null,
    "Reserved": null,
    "PlayFabEnvironment": {
        "Vertical": "master",
        "Cloud": "main",
        "Application": "logicserver",
        "Commit": "a881f0a"
    }
}
Raw event JSON
{
    "EventName": "player_displayname_changed",
    "PreviousDisplayName": null,
    "DisplayName": "undefined",
    "EventNamespace": "com.playfab",
    "EntityType": "player",
    "Source": "PlayFab",
    "TitleId": "BFD0A",
    "EntityId": "1066CF1F71C39EC1",
    "EventId": "52863f8713d945f6acc4393c3f8e480a",
    "SourceType": "BackEnd",
    "Timestamp": "2019-10-10T08:22:51.0230004Z",
    "History": null,
    "CustomTags": null,
    "Reserved": null,
    "PlayFabEnvironment": {
        "Vertical": "master",
        "Cloud": "main",
        "Application": "mainserver",
        "Commit": "a881f0a"
    }
}

UPDATE:

I call VerifyText in my client code and I found out that I don't get the JavascriptException if I change handlers.UpdateDisplayname = function (args, context) to function UpdateDisplayname(NewDesiredDisplayname).

In addition, now the title display name doesn't change to "undefined" if I create/update it. Now, title display name has the correct string.

handlers.VerifyText = function (args, context)
{
    if (VerifyTextLength(args.text) && VerifyTextASCII(args.text))
    {
        UpdateDisplayname(args.text);
     //   handlers.UpdateDisplayname(args.text,context);
        log.info(args.text);
        return "true";
    }
    else
    {
        if (!VerifyTextLength(args.text) && !VerifyTextASCII(args.text))
        {
            log.info(args.text);
            return "too long and characters are not supported";
        }
        else
        {
            if (!VerifyTextLength(args.text))
            {
                log.info(args.text);
                return "too long";
            }
            if (!VerifyTextASCII(args.text))
            {
                log.info(args.text);
                return "characters are not supported";
            }
        }
    }
}

function VerifyTextLength(text)
{
    if (text.length <= 20)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
function VerifyTextASCII(text)
{
    var notSupportArray = new Array();
    for (var i = text.length - 1; i >= 0; i--)
    {
        var charCode = text.charCodeAt(i);
        if ((charCode >= 32 && charCode <= 512) || (charCode >= 8216 && charCode <= 8222) || (charCode >= 8352 && charCode <= 8378))
        {
 
        }
        else
        {
            notSupportArray.push(charCode);
        }
    }
    if (notSupportArray.length == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//handlers.UpdateDisplayname = function (args, context)
function UpdateDisplayname(NewDesiredDisplayname)
{
 // var NewDisplayname = args.DesiredDisplayname;
  var NewDisplayname = NewDesiredDisplayname;
  var CurrentDisplayName = "";
   
   var resultprofile = server.GetPlayerProfile(
       {
           PlayFabID: currentPlayerId, 
           ProfileConstraints : {
               ShowDisplayName: true
               
           }
       });
       
       if (resultprofile.Error == null)
       {
          if (resultprofile.PlayerProfile != null)
          {
            CurrentDisplayName = resultprofile.PlayerProfile.DisplayName;
           
          if ((CurrentDisplayName == null) || (CurrentDisplayName == ""))
           {
             var result =  UpdateUserTitleDisplayNameFromCloudScript(NewDisplayname, currentPlayerId);
             if (result.error == "NameNotAvailable")
             {
               log.info("This display name already exists: " + NewDisplayname.toString() + " Please choose another display name");
       	       return "display name already exists";
             }
             
             if (result.error == "InvalidParams")
             {
               log.info("The Display name must be a string with a minimum length of 3 and a maximum length of 20.");
       	       return "Invalid Parameters";
             }
       	       
       	     log.info("Display name changed");
       	     return NewDisplayname.toString();
           }
          else
            log.info("You already have a display name. It's not possible to change it:" + CurrentDisplayName.toString());
          }
       }
}

function UpdateUserTitleDisplayNameFromCloudScript(DesiredDisplayname, PlayFabId)
{
    var contentBodyTemp = {
        "DisplayName": DesiredDisplayname,
        "PlayFabId": PlayFabId
    };
    
    let url = "https://BFD0A.playfabapi.com/Admin/UpdateUserTitleDisplayName";
    let method = "POST";
    let contentBody = `{"PlayFabId": "${PlayFabId}", "DisplayName": "${DesiredDisplayname}"}`;
    let contentType = "application/json";
    let headers = { "X-SecretKey": "..." };
    let responseString = http.request(url, method, contentBody, contentType, headers);
    let responseJSONObj = JSON.parse(responseString);
    return (responseJSONObj);
}
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.

xolenexil avatar image
xolenexil answered

The problem was in the parameter which script passed to handlers.UpdateDisplayname

handlers.UpdateDisplayname = function (args, context)

	var NewDisplayname = args.DesiredDisplayname

handlers.UpdateDisplayname was expecting "args" to be json with property "DesiredDisplayname" but he received text (string) from "handlers.VerifyText"

handlers.VerifyText=function(args, context)

	handlers.UpdateDisplayname(args.text,context);

JS will return "undefined" when variable (object,json,string...) don't have property.

Script changed "name" before it's throw JavascriptException which was at the end of "UpdateDisplayname", because "NewDisplayname" was "undefined" (args.DesiredDisplayname)

return NewDisplayname.toString();

So in short: script tried to execute "args.text.DesiredDisplayname.toString()" when "text" is string, then "DesiredDisplayname" is undefined, so toString() throw Exception.

,

The problem was in the parameter which script passed to handlers.UpdateDisplayname

handlers.UpdateDisplayname = function (args, context)

	var NewDisplayname = args.DesiredDisplayname

handlers.UpdateDisplayname was expecting "args" to be json with property "DesiredDisplayname" but he received text (string) from "handlers.VerifyText"

handlers.VerifyText=function(args, context)

	handlers.UpdateDisplayname(args.text,context);

JS will return "undefined" when variable (object,json,string...) don't have property.

Script changed "name" before it's throw JavascriptException which was at the end of "UpdateDisplayname", because "NewDisplayname" was "undefined" (args.DesiredDisplayname)

return NewDisplayname.toString();

So in short: script tried to do "args.text.DesiredDisplayname.toString()" when "text" is string, then "DesiredDisplayname" is undefined, so toString() throw Exception.

2 comments
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 ·

It's not working if I only change return NewDisplayname.toString(); to return NewDisplayname; at the end of "UpdateDisplayname". The JavascriptException is gone but the title display name is still undefined.

Raw event JSON
{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "VerifyText",
    "CloudScriptExecutionResult": {
        "FunctionName": "VerifyText",
        "Revision": 67,
        "FunctionResult": "true",
        "FunctionResultTooLarge": null,
        "Logs": [
            {
                "Level": "Info",
                "Message": "Display name changed",
                "Data": null
            },
            {
                "Level": "Info",
                "Message": "Homer S.112",
                "Data": null
            }
        ],
        "LogsTooLarge": null,
        "ExecutionTimeSeconds": 0.1143983,
        "ProcessorTimeSeconds": 0,
        "MemoryConsumedBytes": 55616,
        "APIRequestsIssued": 1,
        "HttpRequestsIssued": 1,
        "Error": null
    },
  ...
    }
}
0 Likes 0 ·
Citrus Yan avatar image Citrus Yan commented ·

That make sense, thanks:)

0 Likes 0 ·
Citrus Yan avatar image
Citrus Yan answered

Hi Kim,

I can see from the first log in line 14 and second log in line 5 that display name was indeed changed, it’s possible that you passed the very “undefined” as the new display name for the UpdateUserDisplayname function. You may need to check your code to see where did that happen. And, it would be helpful if you can provide the repro steps for us to investigate, thanks.

3 comments
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 ·

I found out that I don't get the JavascriptException if I change handlers.UpdateDisplayname = function (args, context) to function UpdateDisplayname(NewDesiredDisplayname).

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Kim Strasser commented ·

Does this solve your issue? Does the "undefined" issue still occur?

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Citrus Yan commented ·

Yes it solved the issue. The title display name is no more "undefined".

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.