question

arvinmediagames2 avatar image
arvinmediagames2 asked

Sending JSON data for executing Cloud Script

I want to send a simple json data (in ActionScript 3) from client for executing cloudScript but when I'm using bellow scripts the result is null

var objStr:Object = JSON.stringify( {
    "score":1,
    "dist":100
	
});


//CloudRequest is an instace of class ExecuteCloudScriptRequest 
//UpdateUserStat is my Cloudscript function name

var CReq:CloudRequest=new CloudRequest("UpdateUserStat",objStr)

This is cloud Script

handlers.UpdateUserStat = function (args)
{
//args.  is JSON data we are passing to function from AS3 (Statistics JSON variable)
    var scoreNum = args.scoreNum;
// "scoreNum" is the JSON number field named "scoreNum" in example above
    var score = args.score; 
 // "score" is the JSON string field named "score"


    var keyString = score;   
    var userStatJSON = {};    //this is creating empty JSON data object
    userStatJSON[score] = scoreNum;    // make sure we are updating levelname string by a number of score


    server.UpdateUserStatistics({   // this is server function which will update user statistic using dataToUpdate
        PlayFabId: currentPlayerId,   // currentPlayerID will be there automatically if player is logged
        UserStatistics: userStatJSON   // this is actual JSON data that we will be updating. (<"levelname", "score">)
    });


    var message =  "score: " + score + " scoreNum: " + scoreNum; 
    return { messageValue: message };


}

apisCloudScript
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.

arvinmediagames2 avatar image arvinmediagames2 commented ·

and this is my EventHistory When calling the function

{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "UpdateUserStat",
    "CloudScriptExecutionResult": {
        "FunctionName": "UpdateUserStat",
        "Revision": 2,
        "FunctionResult": null,
        "FunctionResultTooLarge": null,
        "Logs": [
            {
                "Level": "Error",
                "Message": "PlayFab API request error",
                "Data": {
                    "api": "/Server/UpdateUserStatistics",
                    "request": {
                        "PlayFabId": "E23D765E28E53673",
                        "UserStatistics": {}
                    },
                    "result": null,
                    "apiError": {
                        "code": 400,
                        "status": "BadRequest",
                        "error": "InvalidUserStatistics",
                        "errorCode": 1073,
                        "errorMessage": "Invalid user statistics",
                        "errorHash": null,
                        "errorDetails": null
                    }
                }
            }
        ],

-1 Like -1 ·

1 Answer

·
brendan avatar image
brendan answered

The object you pass in has two parameters - "score" and "dist". But you're attempting to use args.scoreNum in the script, which doesn't exist (there's no "scoreNum" in the object you pass in.

I'd recommend adding more logging to your script, so that you can use that info in your debugging. So, a log.info(args) at the start, so that you can see what you're passing in, and if you have trouble with a Server API call, logging the parameters you're passing in would be good, so that you can confirm your logic.

4 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.

arvinmediagames2 avatar image arvinmediagames2 commented ·

Ok I almost change the script but still doesn't work ... I think the problem is in ActionScript encoding JSON part!

var objStr:Object = JSON.stringify( {
    "score":1
});
//CloudRequest is an instace of class ExecuteCloudScriptRequest //UpdateUserStat is my Cloudscript function name var CReq:CloudRequest=new CloudRequest("UpdateUserStat",objStr)
// Cloud Script (JavaScript)
handlers.UpdateUserStat = function (args) {
    
  var keyString = score;   
  var dataToUpdate = {};    //this is creating empty JSON data object
    
  var objc = JSON.parse(args);
  dataToUpdate[keyString] = objc.score;
  
  server.UpdateUserStatistics({   // this is server function which will update user statistic using dataToUpdate
        PlayFabId: currentPlayerId,   // currentPlayerID will be there automatically if player is logged
        UserStatistics: dataToUpdate   // this is actual JSON data that we will be updating. (<"score">)
    });
    log.info(dataToUpdate.score);

0 Likes 0 ·
brendan avatar image brendan arvinmediagames2 commented ·

In the above Cloud Script code, there is no "score" parameter defined at the start, so attempting to set "keyString" to it will fail. Also, the subsequent call to use it as the Key for a key/value pair will likewise fail.

Next, args is already an object, so there's no need to parse it.

Here's the JavaScript for what it looks like you're trying to do (set the statistic "score" to a value):

handlers.UpdateUserStat = function (args) {


  var dataToUpdate = {};
  dataToUpdate["score"] = args.score;

  server.UpdateUserStatistics({
    PlayFabId: currentPlayerId,
    UserStatistics: dataToUpdate
  });

}
0 Likes 0 ·
arvinmediagames2 avatar image arvinmediagames2 brendan commented ·

I tried your script but its still not working...here's the log (result is null)

 "Level": "Error",
                "Message": "PlayFab API request error",
                "Data": {
                    "api": "/Server/UpdateUserStatistics",
                    "request": {
                        "PlayFabId": "E23D765E28E53673",
                        "UserStatistics": {}
                    },
                    "result": null,
                    "apiError": {
                        "code": 400,
                        "status": "BadRequest",
                        "error": "InvalidUserStatistics",
                        "errorCode": 1073,
                        "errorMessage": "Invalid user 
}
}
0 Likes 0 ·
Show more comments

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.