question

support-2 avatar image
support-2 asked

Cocos Client : how to handle ExecuteCloudScriptResult’s FunctionResult ?

Hello,

I am using the Cocos client. I have a cloud script that is returning a dictionary(small and not very complicated). On the client side what is the recommended way to get info from the FunctionResult of the ExecuteCloudScriptResult ?

Thanks,

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.

jital avatar image
jital answered

Greetings @support-2,

I have found a workaround for your issue.

If you use JSON.stringify before returning your object the ExecuteCloudScriptResult.functionResult will return the proper type.

Example:

handlers.ReturnDictionary = function (args, context)
{    
  var dict = {};


      dict["one"] = 1;


      dict["two"] = 2;


  var dictString = JSON.stringify(dict);
  return dictString;

}

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.

support-2 avatar image support-2 commented ·

Hello,

This workaround worked for me. Thanks.

0 Likes 0 ·
jital avatar image
jital answered

Greetings,

The FunctionResult of the ExecuteCloudScript API call is just a JSON object so you would need a way to parse the string returned as a proper JSON object. But the way to get that string is the following code.

//In success callback function
OnSuccess(const PlayFab::ClientModels::ExecuteCloudScriptResult& res, void* customData)
{
    string jsonString = res.FunctionResult;
    //Handle JSON as needed
}
//Hope this helps
6 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.

support-2 avatar image support-2 commented ·

So, the result.FunctionResult I am getting is a PlayFab::MultitypeError type and therefore I can’t parse it.

void initialDataCallback(const PlayFab::ClientModels::ExecuteCloudScriptResult& result, void* something) {

    printf("initialDataCallback \n");

    if (result.FunctionResult.isNull() == false) {

        printf("initalData is NOT null\n");

        PlayFab::MultitypeVarTypes type = result.FunctionResult.getType();

        if (type == PlayFab::MultitypeError) {
            printf("errorType\n");
        } else {
            printf("not error type\n");
        }

        
        std::string jsonString = result.FunctionResult;
        const char* json = jsonString.c_str();


        rapidjson::Document doc;
        doc.Parse(json);


        if (doc.HasParseError()) {
            printf("has parse error\n");

        }

        if (doc.HasMember("maxFreeTickets")) {
            printf("has member\n");

        }

	}
}

Output:

initialData is NOT null

errorType

has parse error

Assertion failed: (IsObject()), function FindMember, file /Users/…/document.h, line 1154.



Note: PlayFab shows a successful execution of cloud script. And my Java client handles returned dictionary just fine

0 Likes 0 ·
v-humcin avatar image v-humcin ♦ support-2 commented ·

Can you also print out the error information from the result? The relevant information should be found in "result.error", "result.Error.Error", and "result.Error.Message". More information about at this link. Thanks!

https://docs.microsoft.com/en-us/rest/api/playfab/client/server-side-cloud-script/executecloudscript?view=playfab-rest#scriptexecutionerror

0 Likes 0 ·
support-2 avatar image support-2 v-humcin ♦ commented ·

@Hunter McIntyre

Any update?

I posted my response to your comment in the wrong place (it is down below). I placed it as a reply to jital's answer vs. a reply to your comment.

0 Likes 0 ·
Show more comments
support-2 avatar image support-2 commented ·

This is my callback :

void initialDataCallback(const PlayFab::ClientModels::ExecuteCloudScriptResult& result, void* something) {

    printf("initialDataCallback \n");


    if (result.Error) {

        printf("result.Error->Error.c_str() : %s", result.Error->Error.c_str());

        printf("result.Error->Message.c_str() : %s", result.Error->Message.c_str());

        printf("result.Error->StackTrace.c_str() : %s", result.Error->StackTrace.c_str());

    

    } else {

        printf("there is no Error\n");

    }


            

}

This is the output:

initialDataCallback

there is no Error

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.