Parse.com get users from subquery in other class Cloud Code

606 Views Asked by At

I have the class Friends on Parse:

Class Friends{
   "from" : Pointer(_User)
   "to" : Pointer(_User)
   "allowSee" : Boolean
   "block" : Boolean
   "createdAt" : Date
   "updatedAt" : Date
}

I have a Parse.Query where I get my friends, but this is Class Friends not class User. I try do a cicle for get the users in el field "from" but the array users return void

var user = Parse.User.current();
var me = {__type: 'Pointer',className: '_User', objectId: user.id}

var qFriends = new Parse.Query("Friends");
qFriends.equalTo("to", me);
qFriends.equalTo("allowSee", true);
qFriends.find().then(function(results){

    for (i = 0; i < results.length; i++) {
        var uFriendId = results[i].get("from").id;
        var getUserById = new Parse.Query("_User");
        getUserById.equalTo("objectId", uFriendId);

        getUserById.first({
            success: function(user) {
                users.push(user);
            },
            error: function(error){
                console.log(error);
            }
        })
    }

    return users;

}).then(function(friends){
    response.success(friends);
}, function(error){
    response.error();
});

Do exist way of get users by array of objectIds?, example:

var from =["f8dfg3","32fsg5s","43t4gsd"];
var arrUsers = getUser(from); // return array of PFUser with objectId in array from

Thanks,

1

There are 1 best solutions below

0
On BEST ANSWER

Because your Friends class keeps pointers to users, there is a good way, in a single step, to eagerly fetch the related users. Before running the query, add:

qFriends.include("from");
qFriends.include("to");

Upon completion, the related users will be fetched, so you can get at them as follows:

qFriends.find().then(function(results){
    for (i = 0; i < results.length; i++) {
        var friend = results[i];
        var fromUser = friend.get("from");
        var toUser = friend.get("to");
        // these will be fetched, so you can say things like:
        console.log(fromUser.username);

        // and so on

Incidentally, there's no need to build a pointer from the current user to qualify the query, the current user will do just fine as follows:

qFriends.equalTo("to", Parse.User.current());

Also, your goal should be covered with just the include() advice above, but at some point you might need to query the related objects in the results block the way your original post code attempts. The OP code makes two mistakes there: (1) you can just fetch(result.get("from")), the object -- no need to extract the objectId and call either get() or first() as you do. (2) all of those functions just named are asynchronous. The loop that launches them will terminate before any of them begin, so the line return users; will necessarily return nothing. The correct approach is to fill an array with the fetch()-returned promises and then return a new promise from Parse.Promise.when(fetchPromises);