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,
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,
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; }
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
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
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!
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.
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
3 People are following this question.