Sign In Register

How can we help you today?

Start a new topic

metacollection find() returns an empty array

 

 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):

var type = Spark.getData().type; // get the type we passed in
if(type != ""){
    // if the type wasnt an empty string, then we can use the type in our query
    Spark.setScriptData('items', Spark.metaCollection('items').find({ "type" : type }, {"_id" : 0}))
}else{
    // if the type was an empty string, we can forget the query as we just want to return everything
    Spark.setScriptData('items', Spark.metaCollection('items').find());
}


Empty "items" array that is being returned:

{
  "@class": ".LogEventResponse",
  "scriptData": {
    "items": []
  }
}

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

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());
}

 

Login to post a comment