question

ryan-2 avatar image
ryan-2 asked

key value in Title Data being removed???

below is my code.

handlers.checkVersion = function (args1, args2, context) {
    var oldQABuildVersion = Number(args1.oldTestBuildVersion);
    var officialBuildVersion = Number(args2.recentBuildVersion);
    
    var testVersion = server.GetTitleData({Keys : ["OldTestBuildVersion"]})["Data"]["OldTestBuildVersion"];
    if (testVersion.hasOwnProperty["OldTestBuildVersion"])
    {    
        testVersion["OldTestBuildVersion"] = oldQABuildVersion;
    }
    
    var buildVersion = server.GetTitleData({Keys : ["NewTestBuildVersion"]})["Data"]["NewTestBuildVersion"]; 
    if (buildVersion.hasOwnProperty["NewTestBuildVersion"])
    {
        buildVersion["NewTestBuildVersion"] = officialBuildVersion;
    }
    
    
    var setTitleDataRequest = {
        "Key": "OldTestBuildVersion",
        "Value": oldQABuildVersion,
        "Key" : "NewTestBuildVersion",
        "Value": officialBuildVersion
    };
    var setTitleDataResponse = server.SetTitleData(setTitleDataRequest);
 
    // get the key
    var getTitleDataRequest ={ "Keys": ["OldTestBuildVersion"], "Keys": ["NewTestBuildVersion"] };
    var getTitleDataResponse = server.GetTitleData(getTitleDataRequest);
    if (!getTitleDataResponse.Data.hasOwnProperty("OldTestBuildVersion" || !getTitleDataResponse.Data.hasOwnProperty("NewTestBuildVersion"))) {
        log.error("key not found. Exiting...");
        return JSON.stringify([]);
    }
    else {
        return [getTitleDataResponse.Data.OldTestBuildVersion, getTitleDataResponse.Data.NewTestBuildVersion];
    }    
};

I'm trying to return multiple keys. When I return single key - getTitleDataResponse.Data.OldTestBuildVersion - I get correct value. My Title Data doesn't get deleted, everything is fine. When I try to return multiple values like above, I get following error:

{"APIRequestsIssued":2,"Error":{"Error":"JavascriptException","Message":"JavascriptException","StackTrace":"TypeError: Cannot read property 'hasOwnProperty' of undefined\n    at handlers.checkVersion (25E64-main.js:72:22)\n    at Object.invokeFunction (Script:116:33)"},"ExecutionTimeSeconds":0.0211434,"FunctionName":"checkVersion","FunctionResult":null,"FunctionResultTooLarge":null,"HttpRequestsIssued":0,"Logs":[],"LogsTooLarge":null,"MemoryConsumedBytes":31008,"ProcessorTimeSeconds":0,"Revision":42,"Request":{"FunctionName":"checkVersion","FunctionParameter":{"oldTestBuildVersion":0.019999999999999997,"recentBuildVersion":"0.03"},"GeneratePlayStreamEvent":true,"RevisionSelection":null,"SpecificRevision":null,"AuthenticationContext":null},"CustomData":null}

so I checked my Title Data kvp, and found out that my key - NewTestBuildVersion - got removed when I called the function.

What's causing my key to be removed and how can I receive two values?

Following is my c# script for callback on this:

private static void OnCloudHelloWorld(ExecuteCloudScriptResult result)
    {
        // Cloud Script returns arbitrary results, so you have to evaluate them one step and one parameter at a time
        Debug.Log(JsonWrapper.SerializeObject(result.FunctionResult));        
    }
Title Data
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

·
Sarah Zhang avatar image
Sarah Zhang answered

There are several issues in the above CloudScript code. You can check them in the following list.

    1. CloudScript functions can’t have two argsparameter, the function will only take the first parameter as the “args”. It’s pre-defined in the PlayFab CloudScript environment. So, it’s incorrect to use “arg1”,”arg2” to hold two K/V pairs. CloudScript environment will take the first one as “args”, and take the second one as “context”. Hence, the variable officialBuildVersion will be null here. You will also can’t log it via log.info. If you have not learned more about the pre-defined Function Parameters in the CloudScript yet, you can check this title “Writing custom CloudScript -> Intermediate Overview: Globals and advanced argumentsfirst.
    2. The request body of GetTitleData should only contain keys array, other content in it will not take effects. You can write two keys in one request body if you want.
    3. The request body of SetTitleData should contain only one K/V pair. If you write multiple K/V pairs in it, PlayFab won’t return an error but will only set the last one. Then because officialBuildVersion is null and when a value is set to null its K/V pair will be removed. So finally you removed NewTestBuildVersion via your code.
    4. Even if this K/V pair is exited in the Title Data, server should still use SetTitleData API to update it, it’s incorrect to use “testVersion["OldTestBuildVersion"] = oldQABuildVersion;” to update a K/V pair.
    5. After SetTitleData is called successfully, the data on PlayFab Server won’t be updated immediately, there will be a short interval. Please don’t GetTitleData immediately to confirm if it is updated successfully. You can hold the verification function in another handlers function, then call it on clients to verify the title data after an interval.

          You can refer to the following code for more details.

          handlers.UpdateVersions = function (args, context) {
              var oldQABuildVersion = Number(args.oldTestBuildVersion);
              var officialBuildVersion = Number(args.recentBuildVersion);
          
              var setOldTestBuildVersionRequest = {
                  "Key": "OldTestBuildVersion",
                  "Value": oldQABuildVersion,
          
              };
          
              var setNewTestBuildVersionRequest = {
          
                  "Key": "NewTestBuildVersion",
                  "Value": officialBuildVersion
              };
          
              var setTestBuildVersionResponse = server.SetTitleData(setOldTestBuildVersionRequest);
              var setNewTestBuildVersionResponse = server.SetTitleData(setNewTestBuildVersionRequest);
          
              return { result: setNewTestBuildVersionResponse.Data };
          };
          
          handlers.CheckVersions = function (args, context) {
              CheckVersionsFuc(["OldTestBuildVersion", "NewTestBuildVersion"]);
          };
          
          function CheckVersionsFuc(array) {
              var versions = server.GetTitleData({ Keys: array });
          
              if (versions.Data.hasOwnProperty("OldTestBuildVersion")) {
                  var testVersion = versions.Data["OldTestBuildVersion"];
                  log.info("key found, OldTestBuildVersion is " + testVersion);
              } else {
                  log.info("key not found, will create new OldTestBuildVersion");
              }
          
              if (versions.Data.hasOwnProperty("NewTestBuildVersion")) {
                  var buildVersion = versions.Data["NewTestBuildVersion"];
                  log.info("key found, NewTestBuildVersion is " + buildVersion);
              } else {
                  log.info("key not found, will create new NewTestBuildVersion");
              }
          
              return { OldTestBuildVersion: testVersion, NewTestBuildVersion: buildVersion };
          };
          
          
          10 |1200

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

          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.