最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parse Cloud Code relational query syntax - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jan 28, 2015 at 10:01 Alex 11.2k12 gold badges52 silver badges64 bronze badges asked Apr 9, 2014 at 10:53 DarrenDarren 10.4k21 gold badges102 silver badges170 bronze badges 11
  • On your LikeRequest class do you have a user column? I'm not understanding query.notEqualTo("user", Parse.User.current()); – bmurmistro Commented Apr 11, 2014 at 14:59
  • In the bottom section where it adds the Relation, that's adds a many to many user column. – Darren Commented Apr 11, 2014 at 18:03
  • Sorry for the delayed response. Have you tried something like this: var innerQuery = LikeRequest.relation("sendTo").query(); innerQuery.exists(Parse.User); – bmurmistro Commented Apr 14, 2014 at 14:18
  • I'm away for a few days now, so will try when I get back. Thanks – Darren Commented Apr 15, 2014 at 11:22
  • 1 Only now realized this is very old question. Did you ever come to a solution? – Tom Erik Støwer Commented Sep 20, 2014 at 20:06
 |  Show 6 more comments

3 Answers 3

Reset to default 2

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论