Monday, May 20, 2024
136
rated 0 times [  138] [ 2]  / answers: 1 / hits: 16054  / 11 Years ago, wed, july 17, 2013, 12:00:00

I have a Player class. Players can have x number of Trophies. I have the Player objectId and need to get a list of all of their Trophies.



In the Parse.com data browser, the Player object has a column labeled:



trophies Relation<Trophy>
(view Relations)


This seems like it should be so simple but I'm having issues with it.



I have the ParseObject 'player' in memory:



var query = new Parse.Query(Trophy);
query.equalTo(trophies, player);
query.find({
/throws an error- find field has invalid type array.


I've also tried relational Queries:



var relation = new Parse.Relation(player, trophies);
relation.query().find({
//also throws an error- something about a Substring being required.


This has to be a completely common task, but I can't figure out the proper way to do this.



Anyone know how to do this in Javscript CloudCode?



Many thanks!



EDIT--



I can do relational queries on the user fine:



var user = Parse.User.current();
var relation = user.relation(trophies);
relation.query().find({


I don't understand why this very same bit of code breaks if I'm using a non-user object.


More From » parse-platform

 Answers
23

I finally sorted this out, though there is a caveat that makes this work differently than the documentation would indicate.



//Assuming we have 'player', an object of Class 'Player'.

var r = player.relation(trophies);
r.query().find({
success: function(trophies){
response.success(trophies); //list of trophies pointed to by that player's trophies column.
},
error: function(error){
response.error(error);
}

})


The caveat: You must have a 'full' player object in memory for this to work. You can't save a player object, grab the object from the success callback and have this work. Some reason, the object that is returned in the success handler is appears to be an incomplete Parse.Object, and is missing some of the methods required to do this.



Another stumbling block about the Parse.com Javascript SDK- A query that finds nothing is still considered successful. So every time you query for something, you must check the length of the response for greater than 0, because the successful query could have returned nothing.


[#76925] Tuesday, July 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jenamackennac

Total Points: 304
Total Questions: 110
Total Answers: 107

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
jenamackennac questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Wed, Apr 21, 21, 00:00, 3 Years ago
Thu, Apr 1, 21, 00:00, 3 Years ago
Tue, Feb 2, 21, 00:00, 3 Years ago
;