var x = Spark.runtimeCollection("R_Test").find({"player_id":Spark.getPlayer().getPlayerId()});
Spark.setScriptData("testA",x);
Spark.runtimeCollection("R_Test").insert({"player_id":Spark.getPlayer().getPlayerId(),"uid":mUtility().generateUID()});
var y = Spark.runtimeCollection("R_Test").find({"player_id":Spark.getPlayer().getPlayerId()});
Spark.setScriptData("testB",y);
I expect testA to be an empty array and testB to contain my newly inserted query, yet, the response I receive is
Can someone from tech support clue me as to why it happens?
When I try to stringify it using
var x = JSON.stringify(Spark.runtimeCollection("R_Test").find({"player_id":Spark.getPlayer().getPlayerId()}));
Spark.setScriptData("testA",x);
Spark.runtimeCollection("R_Test").insert({"player_id":Spark.getPlayer().getPlayerId(),"uid":mUtility().generateUID()});
var y = JSON.stringify(Spark.runtimeCollection("R_Test").find({"player_id":Spark.getPlayer().getPlayerId()}));
Spark.setScriptData("testB",y);
That is very confusing! But i figured it out after a while.
So, the problem (i think) is that the collection.find() function returns a mongo-cursor. So, even when it should return null, it is return a position. That position would appear to be the same as for when one element is inserted. So when you return the script data is gets the cursor for that collection which by the end of the script contains one element. So it doesn't return null the first time.
So the solution is to use findOne, instead of find as this will return the actual document. Therefore, it will return null if it cant find anything.
As for using JSON.stringify, you will need to tell that method how to serialize the object.
That is very confusing! But i figured it out after a while.
So, the problem (i think) is that the collection.find() function returns a mongo-cursor. So, even when it should return null, it is return a position. That position would appear to be the same as for when one element is inserted. So when you return the script data is gets the cursor for that collection which by the end of the script contains one element. So it doesn't return null the first time.
So the solution is to use findOne, instead of find as this will return the actual document. Therefore, it will return null if it cant find anything.
As for using JSON.stringify, you will need to tell that method how to serialize the object.
Davendra Jayasingam
From my code
I expect testA to be an empty array and testB to contain my newly inserted query, yet, the response I receive is
Can someone from tech support clue me as to why it happens?
When I try to stringify it using
I receive this response
Hey Davendra,
That is very confusing! But i figured it out after a while.So, the problem (i think) is that the collection.find() function returns a mongo-cursor. So, even when it should return null, it is return a position. That position would appear to be the same as for when one element is inserted. So when you return the script data is gets the cursor for that collection which by the end of the script contains one element. So it doesn't return null the first time.
So the solution is to use findOne, instead of find as this will return the actual document. Therefore, it will return null if it cant find anything.
As for using JSON.stringify, you will need to tell that method how to serialize the object.
Hope that helps,
-Sean
- Oldest First
- Popular
- Newest First
Sorted by PopularDavendra Jayasingam
I do believe that you are absolutely right. Thanks Sean
Customer Support
Customer Support
Hey Davendra,
That is very confusing! But i figured it out after a while.So, the problem (i think) is that the collection.find() function returns a mongo-cursor. So, even when it should return null, it is return a position. That position would appear to be the same as for when one element is inserted. So when you return the script data is gets the cursor for that collection which by the end of the script contains one element. So it doesn't return null the first time.
So the solution is to use findOne, instead of find as this will return the actual document. Therefore, it will return null if it cant find anything.
As for using JSON.stringify, you will need to tell that method how to serialize the object.
Hope that helps,
-Sean
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics