1
votes

I have a table called friends with fields userid and friendid.

I want to query the database to find a user's friends. This is working fine by using the following code:

Parse.Cloud.define("searchfriend", function(request, response) {
  var query = new Parse.Query("friends");
  query.equalTo("player", request.params.myid);
  query.find({
    success: function(results) {
      var listfreundids = [];
      for (var i = 0; i < results.length; ++i) {
        listfreundids[i] = results[i].get("friend");;
}
      response.success(listfreundids);
    },
    error: function() {
      response.error("error");
    }
  });
});

Now I have the problem to find the username matching the friendid because i cannot use a 2nd query within the for loop to query the user database...

2

2 Answers

1
votes

Using promises you can split this up into several separate parts. Promises are really awesome to use and really easy to create your own promises too.

What I would do is split this up into a query that finds the friend ids and then a query that finds the friends...

Parse.Cloud.define("searchfriend", function(request, response) {
  getFriendIDs(request.params.myid).then(function(friendIDs) {
    return getFriendUserNames(friendIDs);
  }).then(function(friends) {
    response.success(friends);
  }), function(error) {
    response.error(error);
  });
});

// function to get the IDs of friends
function getFriendIDs(myID) {
  var promise = new Parse.Promise();

  var query = new Parse.Query("friends");
  query.equalTo("player", myID);
  query.find().then(function(friendIDs) {
    promise.resolve(friendIDs);
  }, function(error) {
    promise.reject(error);
  });

  return promise;
}

// function to get the friends from a list of IDs
function getFriendUserNames(friendIDs) {
  var promise = new Parse.Promise();

  var query = new Parse.Query("_User");
  query.containedIn("id", friendIDs);

  query.find().then(function(friends) {
    // here I am just returning the array of friends
    // but you can pull the names out if you want.
    promise.resolve(friends);
  }, function(error) {
    promise.reject(error);
  });

  return promise;
}

You could always user a matches query too...

// function to get friends
function getFriends(myID) {
  var promise = new Parse.Promise();

  var friendQuery = new Parse.Query("friends");
  friendQuery.equalTo("player", myID);

  var userQuery = new Parse.Query("User");
  userQuery.matchesKeyInQuery("ID", "friendID", friendQuery);

  userQuery.find().then(function(friends) {
    promise.resolve(friends);
  }, function(error) {
    promise.reject(error);
  });

  return promise;
}

This will perform a joined query where it gets the friend IDs first and then uses the friend ID to get the user and returns the user object.

Also, use promises. They are much easier to work with and can be pulled apart into separate working units.

Of course, I have no idea if the syntax here is correct and what the correct names should be or even what your object model looks like but hopefully this can act as a guide.

0
votes

You do not have to query for each friend id that you have found (inefficient). Instead, after getting the list of friend ids, you can query the user database via sending the list of friend ids with the query.containedIn constraints. This strategy will actually decrease the number of query count.Based on retrieved result you can get friend (previously found) information. One more thing to remember, call response success after the operations are executed.Hope this helps.

Regards.