question

Damian Rajamanie avatar image
Damian Rajamanie asked

Cannot UpdateUserData with a new Key/Value from Cloud Script

I'm trying to write an Admin routine to add a new key and value to 'some' users (specifically top 100 on our leaderboard).

All indicators, clouscript logs in the PlayStream, return values show everything to be ok, bu the key is not getting added at all.

This is my cloud script (I've got rid of irrelevent stuff):

handlers.setPrizes = function (args, context){
                
var id = args.playFabID;
var prizes = args.prizes;
log.info("id: " + id + ", prizes: " + prizes);
try{
server.UpdateUserData({ "PlayFabId" : id, "Data" : [{ "prizes" : prizes }] });
}catch (ex){
..stuff..
};

The arg.PlayfabID is correct as to the user that I want to add this key to

args.prizes is a string, not an object and looks like this: {\"MaskPrizes\":{\"MaskIndex\":13,\"Owned\":[13,12,11]}}

The log in the playstream looks thus: ( Have ?? out the characters of the playfab user id)

"Logs": [
            {
                "Level": "Info",
                "Message": "id: E2D3?????????, prizes: {\"MaskPrizes\":{\"MaskIndex\":13,\"Owned\":[13,12,11]}}",
                "Data": null
            },

As there are no errors and it all looks ok, I'm at a loss :(

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

·
Damian Rajamanie avatar image
Damian Rajamanie answered

hmm, I found some stuff on the forums:

https://community.playfab.com/questions/496/206963508-Writing-to-a-dynamic-Key-in-Cloud-Script.html

and I think I got it working, I changed the inner portion of my try statement to do what's suggested, like this:

try{
      var dataPayload = {};
      dataPayload["prizes"] = args.prizes;

      server.UpdateUserData({
          "PlayFabId" : id,
          "Data" : dataPayload
        }); 

}
catch (ex){
                

This seems to be positing, either adding or editing, still testing that out, but that looks like the solution

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.

Damian Rajamanie avatar image Damian Rajamanie commented ·

Not sure that this is clear on any of the documentation for Cloud Script or UpdateUserData, the samples are more basic.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ commented ·

The thing is you cannot set a list in Data, you may try:

handlers.setPrizes = function (args, context){
   var id = args.playFabID; 
   var prizes = args.prizes; 
   log.info("id: " + id + ", prizes: " + prizes);
  try{
        
      server.UpdateUserData({
		"PlayFabId" : id,
		"Data": {
    			"Class": "Fighter",
    			"Gender": "Female",
    			"Icon": "Guard 3",
    			"Theme": "Colorful"
 		},

          });  
  }catch (ex){
    ..stuff..
};
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.