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?
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?
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?
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:
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"}; }
3 People are following this question.