Sign In Register

How can we help you today?

Start a new topic

Can't debug objects in cloud code

Hi,

I am trying to debug a cloud code script.

I have some javascript object i am manipulating and i am trying to print their contents with 

Spark.getLog().debug("my obj: " + myObj);

  

if i print myObj as-is, i get "My Obj = [object Object]"

but if i try to JSON.stringify it, i get this exception

  

{"stackTrace":"\tat event/***.js:64\n","error":"Java class \"[Ljava.lang.String;\" has no public instance field or method named \"toJSON\". (event/***.js#64)","script":"***"}

 probably due to some native stuff i am loading inside

 (myObj contains playerId and displayName of the player loaded with Spark.getPlayer() among other stuff).



Hi Mauro,


Do you know which field in particular is causing this error? If you prepend the value of this field with an empty string (turning it into a js string), it should resolve the issue:


myObj.myField = ''+myObj.myField;


How large is your object? Does it contain nested objects? For debugging a simple object you could iterate over the object's keys and print the values individually:


var keys = Object.keys(myObj);

for(var i =0, l=keys.length;i<l;++i){

     Spark.getLog().debug(i + ": " + myObj[i]);

}


Please let us know if either of these solutions resolves this issue for you.


Regards,

Vinnie

my obj is ~30ish fields, some of them are nested or arrays.

it actually is a representation of player, with its properties defined with

Object.defineProperties(user, properties.reduce(function(obj, k){
                    obj[k] = {
                        get:function(){return player.getPrivateData(k);},
                        set:function(value){player.setPrivateData(k, value);}
                    };
                    return obj;
                }, {}));

 for my custom data, and some default mappings like 

{
    ...
    get Coins() { return player.getBalance1();},
    get Friends() {
                        var t = Spark.getTeams().getTeamByOwnerIdAndTeamType(player.getPlayerId(),"friendsList");
                        if (t.length > 0)
                            return t[0].getMemberIds();
                        return [];
                    },
    ...
}

after some tests, i found the problem is due to native array of strings, and i managed to work around it with 

var r = [];
if (t.length > 0)
    r.push (t[0].getMemberIds());
return r;

 (native strings are working as expected)

ps: if I send it to the client with Spark.SetScriptData("debug", myObj) I correctly get all the fields, including the native ones.

Login to post a comment