Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Cloud Code relational query syntax

I have a cloud function on Parse. When it's called it retrieves a PFObject then adds a relation between that object and the user. This part works fine (Seen towards the end of the function). I'm having trouble getting the query that selects the PFObject to ignore those that the user is already related to

Here is my code:

Parse.Cloud.define("NextMedia", function (request, response) {      var LikeRequest = Parse.Object.extend("LikeRequest");     var query = new Parse.Query(LikeRequest);      query.equalTo("completed", false);     console.log("user: " + Parse.User.current().id);      query.notEqualTo("user", Parse.User.current());      // Also only fetch if never been sent before      // HERE SHOULD USE THE BELOW RELATIONSHIP     var innerQuery = new Parse.Query(Parse.User);     innerQuery.exists(Parse.User);     query.matchesQuery("sentTo", innerQuery);      query.ascending("createdAt");      query.first({          success: function (object) {             // Successfully retrieved the object.             console.log("Got 1 object: " + object.get('mediaId'));              // Record that the user has been sent it             var user = Parse.User.current();             var relation = object.relation("sentTo"); // RELATION ADDED HERE              relation.add(user);             object.save();              response.success(object);         },         error: function (error) {             console.log("Error: " + error.code + " " + error.message);              response.error("Error getting next mediaId");         }     }); }); 

I'm sure i'm just not understanding how the relation query syntax works.

like image 235
Darren Avatar asked Apr 09 '14 10:04

Darren


1 Answers

This stretch:

// Also only fetch if never been sent before  // HERE SHOULD USE THE BELOW RELATIONSHIP var innerQuery = new Parse.Query(Parse.User); innerQuery.exists(Parse.User); query.matchesQuery("sentTo", innerQuery); 

Could be changed to:

// Also only fetch if never been sent before  query.notContainedIn("sentTo",[Parse.User.current()]) 

That works.Parse Query

like image 174
Oscar Eduardo Avatar answered Sep 23 '22 15:09

Oscar Eduardo