我在 Parse 上有“Friends”类(class):
Class Friends{
"from" : Pointer(_User)
"to" : Pointer(_User)
"allowSee" : Boolean
"block" : Boolean
"createdAt" : Date
"updatedAt" : Date
}
我有一个 Parse.Query 来获取我的 friend ,但这是类 friend 而不是类用户。我尝试做一个 cicle 来获取 el 字段“from”中的用户,但数组用户返回 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();
});
是否存在通过 objectId 数组获取用户的方法?例如:
var from =["f8dfg3","32fsg5s","43t4gsd"];
var arrUsers = getUser(from); // return array of PFUser with objectId in array from
谢谢
最佳答案
因为您的 Friends 类保留指向用户的指针,所以有一个好方法,一步即可急切地获取相关用户。在运行查询之前,添加:
qFriends.include("from");
qFriends.include("to");
完成后,将获取相关用户,因此您可以按如下方式获取它们:
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
顺便说一句,不需要从当前用户构建指针来限定查询,当前用户将执行如下操作:
qFriends.equalTo("to", Parse.User.current());
此外,您的目标应该只包含上面的 include()
建议,但在某些时候,您可能需要查询结果中的相关对象,从而阻止原始邮政代码尝试的方式。 OP 代码在那里犯了两个错误:(1) 你可以只 fetch(result.get("from"))
,对象 - 不需要提取 objectId 并调用 get ()
或 first()
就像你一样。 (2) 刚才提到的所有这些函数都是异步的。启动它们的循环将在它们开始之前终止,因此 return users;
行必然不会返回任何内容。正确的方法是用 fetch()
返回的 Promise 填充数组,然后从 Parse.Promise.when(fetchPromises);
关于javascript - Parse.com 从其他类 Cloud Code 中的子查询获取用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30788763/