How to Parse Cloud Code relational query syntax

  Question: I have a cloud function on Parse. When it's called it retrieves a PFObject then adds a relation between tha...

How to Parse Cloud Code relational query syntax newvijay






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

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



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




Answer:

I think Kerem has it partially correct but notContained in is not dynamic enough for your case. You need to construct the query from the relation and then use doesNotMatchKeyInQuery to exclude those objects from your outer query.

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()])


what about Query Constraints?

If you want to retrieve objects that do not match any of several values you can use notContainedIn

  // Finds objects from anyone who is neither Jonathan, Dario, nor Shawn
query.notContainedIn("playerName",
                     ["Jonathan Walsh", "Dario Wunsch", "Shawn Simon"]);

COMMENTS

Name

Android Apple Apps Gadget Gaming How Tos Laptops Latest Latets news Linux Microsoft New Movies New phones New Tricks Nokia Offers PC Programing ROMs Root Softwares Solution SONY Symbian^3 Themes TOP 10 HD iPad 2 Games Whatsapp
false
ltr
item
New Vijay: Leaked Online Loot and Offers : How to Parse Cloud Code relational query syntax
How to Parse Cloud Code relational query syntax
http://1.bp.blogspot.com/-BcjvXY0e3Sg/VZdNh26pGDI/AAAAAAAAD8A/C5gg_rWGZG0/s320/How%2Bto%2BParse%2BCloud%2BCode%2Brelational%2Bquery%2Bsyntax-newvijay.png
http://1.bp.blogspot.com/-BcjvXY0e3Sg/VZdNh26pGDI/AAAAAAAAD8A/C5gg_rWGZG0/s72-c/How%2Bto%2BParse%2BCloud%2BCode%2Brelational%2Bquery%2Bsyntax-newvijay.png
New Vijay: Leaked Online Loot and Offers
http://newvijay.blogspot.com/2015/06/how-to-parse-cloud-code-relational.html
http://newvijay.blogspot.com/
http://newvijay.blogspot.com/
http://newvijay.blogspot.com/2015/06/how-to-parse-cloud-code-relational.html
true
7835349826098902170
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy