Advertisement

GameSparks JSON MongoDB syntax issues? I am not getting back anything from my query

Started by May 29, 2017 12:09 AM
4 comments, last by GameSparks_Clare 7 years, 6 months ago
Anyone here familiar with GameSparks?
I am trying to make a collection (don't know if I am doing it right I am not well versed in JSON yet):

Spark.save("characterCollection", {
"characters" : [
{
"id" : 1,
"name" : "spec1",
"hitPoints" : 5,
"movement" : 3,
"strength" : 3,
"defense" : 3,
"range" : 1,

"sprite" : "spec1",
"avatar" : "spec1"
},
{
"id" : 2,
"name" : "spec2",
"hitPoints" : 5,
"movement" : 3,
"strength" : 3,
"defense" : 3,
"range" : 1,

"sprite" : "spec2",
"avatar" : "spec2"

}
]
});
Then I am trying to get the data from by searching via name "spec1", again I don't know if I am doing it right not familiar with that either >.<"

var characterList = Spark.metaCollection("characterCollection"); // this one works fine i think

var result = Spark.metaCollection('characterCollection').findOne({"characters" :{ "name" : "spec1"}});
Spark.getLog().debug(result); // result listed as null

Not familiar, but typically calls tell about failures, does "Spark.save" return anything useful?

Advertisement

Spark.save returns a boolean for success/failure and the below code goes through success branch.

i changed the JSON formatting a bit to something i think is closer to right:

if(Spark.save("characterCollection", {
"_id" : 0,
"name" : "spec1",
"hitPoints" : 5,
"movement" : 3,
"strength" : 3,
"defense" : 3,
"range" : 1,

"sprite" : "spec1",
"avatar" : "spec1"
}))
{
Spark.getLog().debug("added");
}
else
{
Spark.getLog().debug("not added");
}

var cursor = Spark.runtimeCollection("characterCollection").find();
var current = cursor.curr();

in the debugger type thingy called test harness it shows "current" as null in this version.

Hi Mercutio604

It looks like you're saving a single document with a single field ('characters'), which contains an array of objects. The query's failing because there is no matching document in the collection
If you change your Spark.save() stuff to:
Spark.runtimeCollection("characterCollection").insert([
{"id":1,
....
},
{"id":2,
....
},
...
]);

it should do what you want (i.e. create a separate document in the collection for each element of the array).
If not, or if you have any other questions about using GameSparks, please do not hesitate to get in touch with our support team via https://support.gamesparks.net/support/home we'd be happy to help in any way we can!
Hi Clare, thanks for the reply. I still cannot get it to work with your version - 'current' shows as null I posted your version of the code as a reply in the link below. https://support.gamesparks.net/support/discussions/topics/1000084527?page=1

Hi Mercutio604,

Sorry I hadn't seen the topic you had already started on our forums. I'll ask one of the team to have a look at it for you!

Clare

This topic is closed to new replies.

Advertisement