In my quest to discover new users who I have not connected with before using a cloud function, I am utilizing several tables:
- Users table - This standard table includes an additional "hasLight" boolean column
- Connected table:
- 'user' - a pointer to the user (first side)
- 'userId' - objectId of the user
- 'user2' - a pointer to the user we are connecting with
- 'userId2' - objectId of the user we connected to
- Available table:
- 'userObjectId' - objectId of the user
- 'counter' - number
The code snippet:
Parse.Cloud.define("findFreshUser", function(request, response) {
Parse.Cloud.useMasterKey(); // For reading all the tables //
var user = Parse.User.current();
// Retrieve all potential users we have previously connected with //
var connectQuery = new Parse.Query("Connected");
connectQuery.include('userId2');
connectQuery.equalTo("userId", Parse.User.current().id);
// Find all users with an availability counter of '0' //
var availableQuery = new Parse.Query("Available");
availableQuery.notEqualTo("counter", 0);
var freshUserQuery = new Parse.Query(Parse.User);
freshUserQuery.doesNotMatchKeyInQuery("objectId", "userId2", connectQuery); // To find users we haven't connected with before - THIS PART ISN'T WORKING!!! //
freshUserQuery.doesNotMatchKeyInQuery("objectId", "userObjectId", availableQuery); // Exclude users with '0' availability - THIS PART IS WORKING //
freshUserQuery.equalTo("hasLight", false); // Users must not have a light //
freshUserQuery.descending("updatedAt");
freshUserQuery.limit(1); // We only need one 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 is ignoring the connectQuery when both doesNotMatchKeyInQuery statements are used together:
If I use only
freshUserQuery.doesNotMatchKeyInQuery("objectId", "userId2", connectQuery);
and comment out
freshUserQuery.doesNotMatchKeyInQuery("objectId", "userObjectId", availableQuery);
it works. It seems like there may be a conflict between using both at the same time. How can I make them both apply?
This issue appears to be related to Parse, but as someone new to CloudCode, I may not be implementing it correctly.
Note: It's worth noting that instead of comparing the user object itself, I am comparing their IDs as part of troubleshooting. I understand there are ways to improve the code and database design for better efficiency.