question

sanjayhillstudios@gmail.com avatar image
sanjayhillstudios@gmail.com asked

Not able to set player data via cloud script

Hello my name is sanjay.I created a cloudscript function called setMapData.

Here is the definition for this:

handlers.setMapData=function(args,context) {

var keyname=args.parametername;

server.UpdateUserData

({ PlayFabId:currentPlayerId,

Data:{ keyname:JSON.stringify(args.data)

}

});

};

I am sending parameters like this:

{

"parametername": "map1alien",

"data":

{

"0":

{

"type": "trap1",

"x": "-1317.419312",

"y": "-158.157776",

"z": "228.536713"

}

}

}

But inside my playerdata INSTEAD OF FILLING THE DATA IN "map1alien" field IT IS CREATING A

NEW FIELD CALLED "keyname" and putting data inside it.PLz help me.Thank you

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.

1 Answer

·
Seth Du avatar image
Seth Du answered

You need to use a feature named “computed property name” in JavaScript so that the input variable value will be used as object key name. Please find reference on Object initializer - JavaScript | MDN (mozilla.org) and replace the function with my fixed one:

handlers.setMapData = function (args, context) {
    var keyname = args.parametername;
    server.UpdateUserData
        ({
            PlayFabId: currentPlayerId,
            Data: {
                [keyname]:JSON.stringify(args.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.

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.