Parse Cloud Code Conundrums - sahajss/knowledge_base GitHub Wiki

#Cloud Code Conundrums My project required both a large database of users, and a single user to be able to iterate through all other users and compare attributes between them. This could be done by querying for all users, storing that locally, then looping over them all. The only issue with this is that it would be very slow for the user of my app, and the user doesn't really want all that information on their phone. To solve this problem of being able to compare a user with all other users in the database I did some research into Parse's Cloud Code. #What is Cloud Code? Cloud Code simply put is javascript that runs on the database. A program can run the cloud code function locally, it will send the functions arguments to the cloud, and the Cloud Code will use those parameters and query through the full database on the server, then send back whatever information the function calls for. The benefits of using Cloud Code are that it returns the information the user wants much quicker, and that it only sends the information that the user wants to receive, instead of the full database. #Cloud Code in my project I used Cloud Code functions to compare a users interests with the interests of all the other users in the database, and return possible people the user might want to be friends with based off their interests. I plan on also using cloud code to query for local events the user might be interested in.


My Javascript:

This function just calls my cloud code, and feeds it some arguments for user's id, and a list of their interests

Parse.Cloud.run('findFriends', {
  curUser: user.id,
  ints: user.get("interests")
}, {
  success: function(results) {
    for (var x = 0; x < results.length; x++) {
      if (friendsList.indexOf(results[x]) < 0) {
        addOption(results[x]);
      }
    }
  },
  error: function(error) {
    console.log("error: " + error.message);
  }
});

My Cloud Code Function:

This is the actual cloud code function, that looks through the database and compares all users to the user it receives, and then returns a list of users that have a certain amount of interests in common with the original user.

 Parse.Cloud.define("findFriends", function(request, response) {
 var userID = request.params.curUser;
 var interests = request.params.ints;
 var query = new Parse.Query(Parse.User);
 var friendsList = [];
 if (interests.length > 0)
   query.find().then(function(results) {
     if (results.length == 0) {
       response.error("error");
     } else {
       for (var i = 0; i < results.length; i++) {
         var friendInts = results[i].get("interests");
         var same = 0;
         if (friendInts) {
           for (var y = 0; y < friendInts.length; y++) {
             if (interests.indexOf(friendInts[y]) > -1) {
               same += 1;
             }
           }
         }
         var threshold = 2;
         if (same >= threshold) {
           if (results[i].id != userID) {
             friendsList.push(results[i]);
           }
         }
       }
       response.success(friendsList);
     }
   });
   });