Idan
Idan

Reputation: 9940

CloudCode multiple query in query doesn't work

I'm trying to find users I didn't connect to before with a cloud function. There are few tables I'm using:

  1. Users table - The standard table with an added "hasLight" boolean column
  2. Connected table:
    • 'user' - pointer to the user (first side)
    • 'userId' - objectId of the user
    • 'user2' - pointer to the user we connect with
    • 'userId2' - objectId of the user we connected to
  3. Available table:
    • 'userObjectId' - objectId of the user
    • 'counter' - number

The code:

Parse.Cloud.define("findFreshUser", function(request, response) {

    Parse.Cloud.useMasterKey(); // For reading all the table //

    var user = Parse.User.current();

    // Get all possible users we already connected to //
    var connectQuery = new Parse.Query("Connected");
    connectQuery.include('userId2');
    connectQuery.equalTo("userId", Parse.User.current().id);

    // Get all users with availability of '0' //
    var availableQuery = new Parse.Query("Available");
    availableQuery.notEqualTo("counter", 0);  

    var freshUserQuery = new Parse.Query(Parse.User);

    freshUserQuery.doesNotMatchKeyInQuery("objectId", "userId2", connectQuery); // We haven't connected before - THIS DOEN'T WORK !!! //
    freshUserQuery.doesNotMatchKeyInQuery("objectId", "userObjectId", availableQuery); // We won't use '0' availability users - THIS WORKS //

    freshUserQuery.equalTo("hasLight", false); // user must not have a light //
    freshUserQuery.descending("updatedAt");
    freshUserQuery.limit(1); // We need only 1 user //

    freshUserQuery.find({ 
        success: function(results) {
            if (results.length>0){
                console.log("Found the user "+ results[0].id);
                response.success(results[0].id);
            }else{
                response.error("No user found");
            }

        },
        error: function() {
            response.error("No user found");
        }
    });

});

For some reason CloudCode completely ignoring connectQuery (all other statements are fine) when using 2 doesNotMatchKeyInQuery statements:

When using only

freshUserQuery.doesNotMatchKeyInQuery("objectId", "userId2", connectQuery);

and comment out

freshUserQuery.doesNotMatchKeyInQuery("objectId", "userObjectId", availableQuery);

it does work. So I think it is related to using both at same time, they are probably conflicting each other. What do I need to do to make both apply?

It feels like it's some parse issue but I'm really new to CloudCode so it's probably something I'm not doing right.

Note: Pay attention that I even don't compare the user object itself but the id of it (this is as part of isolating the issue). Means, I know I could make the code and DB lots nicer.

Upvotes: 11

Views: 125

Answers (1)

Toucouleur
Toucouleur

Reputation: 1242

You have to use Promise to achieve such query : blog.parse.com/learn/engineering/whats-so-great-about-javascript-promises/

Upvotes: -2

Related Questions