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,
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:
Upon completion, the related users will be fetched, so you can get at them as follows:
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:
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 justfetch(result.get("from"))
, the object -- no need to extract the objectId and call eitherget()
orfirst()
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 linereturn users;
will necessarily return nothing. The correct approach is to fill an array with thefetch()
-returned promises and then return a new promise fromParse.Promise.when(fetchPromises);