But when I add this to my event's Cloud Code, in attempt to
retrieve a list of the items, it only gives me a response with an empty
array.
Cloud code used (copied from example):
vartype= Spark.getData().type; // get the typewe passed inif(type!= ""){
// if the typewasnt an empty string, then we can use the typein our query
Spark.setScriptData('items', Spark.metaCollection('items').find({ "type" : type }, {"_id" : 0}))
}else{
// if the typewas an empty string, we can forget the query as we just want to return everything
Spark.setScriptData('items', Spark.metaCollection('items').find());
}
I've already followed the guide and created several mock items
through Data Explorer, and I am able to query them fine within Data
Explorer. But I just can't access them through Cloud Code at all.
I also made sure to "Configure Data Indexes" and added "name" and "type" to "Indexed Fields" in "items".
Would any one have any idea what else might I be missing? I'm sure it's something fairly obvious... Thank you!
1 Comment
A
Andrew Howe
said
over 4 years ago
find() returns a SparkMongoCursor. You need something like this:
var cursor = Spark.metaCollection('items').find({ "type" : type }, {"_id" : 0}) // or whatever
var list = [];
while (cursor.hasNext()) {
list.push(cursor.next());
}
Wynne Lo
Hi, I'm trying to follow this example to create an inventory system: https://docs.gamesparks.com/tutorials/database-access-and-cloud-storage/saving-and-loading-player-inventory-data.html#inserting-new-items
But when I add this to my event's Cloud Code, in attempt to retrieve a list of the items, it only gives me a response with an empty array.
Cloud code used (copied from example):
Empty "items" array that is being returned:
I've already followed the guide and created several mock items through Data Explorer, and I am able to query them fine within Data Explorer. But I just can't access them through Cloud Code at all.
I also made sure to "Configure Data Indexes" and added "name" and "type" to "Indexed Fields" in "items".
Would any one have any idea what else might I be missing? I'm sure it's something fairly obvious... Thank you!