question

JJ avatar image
JJ asked

Append to User Data

Hi, are there any ways for me to append to user data in Playfab? Or do I have to get the old data and then append it to my new data and update it altogther?

Player Datadata
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.

JJ avatar image JJ commented ·

Just asking in advanced!
I am trying to get the old data and append the new data using concat method but it doesnt seem to be working

Cloud Script Code

handlers.UpdateUserData = function(args, context){
    var myData = args.myData;
    var FriendPlayFabId = args.FriendPlayFabId;
    var result = server.GetUserData({
            PlayFabId:FriendPlayFabId
        });
    if(result.Data != null)
        {                      
            var oldRecord = result.Data         
            var myDataCombinedJSONObj = myData.concat(oldRecord); 
            var myData = JSON.stringify(myDataCombinedJSONObj);
            
        }
    server.UpdateUserData({
        PlayFabId: FriendPlayFabId,
        Data: myData
    });    
    return {messageValue: message};
}

However, I am getting an error that myData.concat is not a function. May I please seek advice to move forward?

1 Like 1 ·

1 Answer

·
Rick Chen avatar image
Rick Chen answered

What do you mean by append to user data? Do you mean creating the new KVP (Key/Value pair) for in the user data OR append the content to a Key’s value?

If it is the former, you can use the UpdateUserData API directly, this API will create new KVPs and overwrites the old KVPs.

If it is the latter, you can do that in the following steps:

  1. Get the Value of the key that you want to append new data to using GetUserData API.
  2. Use the string.concat to add the new value to old value.
  3. Use the UpdateUserData API to overwrite the old KVP.

The concat method should be used for string. The error “myData.concat is not a function” indicates that “myData” is not a string. In addition, the “oldRecord” in your code is an object rather than a string and cannot be used in the concat method. For example, if there is a key named “CustomKey” and you want to append a string “NewData” as the prefix of the value, a correct version could be:

handlers.UpdateUserData2 = function(args, context){
    // var myData = args.myData;
    var FriendPlayFabId = args.FriendPlayFabId;
    var result = server.GetUserData({
            PlayFabId:FriendPlayFabId
        });
    if(result.Data != null)
        {                      
            var oldRecord = result.Data.CustomKey.Value;
            var myDataCombinedJSONObj = "NewData".concat(oldRecord);
            var myData = {CustomKey: myDataCombinedJSONObj};
            
        }
    server.UpdateUserData({
        PlayFabId: FriendPlayFabId,
        Data: myData
    });    
    return {messageValue: "success"};
}


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.