Sign In Register

How can we help you today?

Start a new topic
Answered

Sending document as a parameter for an event to insert into a collection

Hello,


There is an object I want to add into a collection  on Unity. I created a JSON from that object with this code and tried to send an event. 

        GameSparks.Api.Requests.LogEventRequest request = new GameSparks.Api.Requests.LogEventRequest();
        request.SetEventKey("MY_EVENT");
        request.SetEventAttribute("MY_COLLECTION", "MyCollection");
        request.SetEventAttribute("MY_DOCUMENT", JsonUtility.ToJson(myObject));
        request.Send(...)

However, on the cloud code, I could not managed to insert that JSON document into the collection "MyCollection"

 

var collectionName = Spark.getData().MY_COLLECTION
var doc = Spark.getData().MY_DOCUMENT
var collection = Spark.runtimeCollection(collectionName);
collection.insert(doc)

The error I get is

Cannot convert to org.mozilla.javascript.NativeObject (299640-event-MY_EVENT_NAME#10) 

 

Note: I've changed the code to describe the problem clearly.

 



Best Answer

Hi Adrian,


You might try stringify it on the server side, than replace out all the non valid characters, seems to be mainly the backslashes, than parse it to JSON.


Hope this helps,

 - Steve 


1 person has this question

Hi Ali,


This problem you are having is happening as a result of JsonUtility.ToJson(myObject), use GSDataRequest instead, you can see the documentation with an example here.


Hope this helps,

- Tony.

I am having the same issue. Unfortunately, I don't control how the request is sent. Is there anything I can do on the GameSparks side? Here is my error:


  "error": "Cannot convert {\"Properties\":[],\"Name\":\"\",\"Children\":[{\"Properties\":[{\"Name\":\"rowCount\",\"Value\":1},{\"Name\":\"columnCount\",\"Value\":13},{\"Name\":\"0-1-name\",\"Value\":\"gameId\"},{\"Name\":\"0-1-type\",\"Value\":1},{\"Name\":\"0-2-name\",\"Value\":\"playerOne\"},{\"Name\":\"0-2-type\",\"Value\":1},{\"Name\":\"0-3-name\",\"Value\":\"playerTwo\"},{\"Name\":\"0-3-type\",\"Value\":1},{\"Name\":\"0-4-name\",\"Value\":\"move1\"},{\"Name\":\"0-4-type\",\"Value\":2},{\"Name\":\"0-5-name\",\"Value\":\"move2\"},{\"Name\":\"0-5-type\",\"Value\":2},{\"Name\":\"0-6-name\",\"Value\":\"move3\"},{\"Name\":\"0-6-type\",\"Value\":2},{\"Name\":\"0-7-name\",\"Value\":\"move4\"},{\"Name\":\"0-7-type\",\"Value\":2},{\"Name\":\"0-8-name\",\"Value\":\"move5\"},{\"Name\":\"0-8-type\",\"Value\":2},{\"Name\":\"0-9-name\",\"Value\":\"move6\"},{\"Name\":\"0-9-type\",\"Value\":2},{\"Name\":\"0-10-name\",\"Value\":\"move7\"},{\"Name\":\"0-10-type\",\"Value\":2},{\"Name\":\"0-11-name\",\"Value\":\"move8\"},{\"Name\":\"0-11-type\",\"Value\":2},{\"Name\":\"0-12-name\",\"Value\":\"move9\"},{\"Name\":\"0-12-type\",\"Value\":2},{\"Name\":\"0-13-name\",\"Value\":\"status\"},{\"Name\":\"0-13-type\",\"Value\":2}],\"Name\":\"id731395_headers\",\"Children\":[]},{\"Properties\":[{\"Name\":\"1\",\"Value\":\"|345|w2343|w23456|2|0|0|0|0|0|0|0|0|0|\"}],\"Name\":\"id731395\",\"Children\":[]}]} to org.mozilla.javascript.NativeObject (callbacks/multiplayer.js#11)",
Answer

Hi Adrian,


You might try stringify it on the server side, than replace out all the non valid characters, seems to be mainly the backslashes, than parse it to JSON.


Hope this helps,

 - Steve 

It might be also worth checking out this tutorial serializing c# objects directly to GSData before sending them...
https://docs.gamesparks.com/tutorials/database-access-and-cloud-storage/c-sharp-object-serialization-for-gamesparks.html

 

Login to post a comment