有一种方法可用于include
指针值的属性。您不能在关系中使用include
方法。我所做的是使用云代码函数将我想要的结果聚合到JSON对象并返回该对象。
请参阅以下脚本中的fetchPostDetails
函数。
https://github.com/brennanMKE/PostThings/blob/master/Parse/PostThings/cloud/main.js
它获取项目有标签的关系等对象,喜欢它碰巧是User
对象它们关系到Post
类。也有评论被引用作为从每条评论回到帖子的指针。方法fetchPostTags
和fetchPostLikes
显示如何获取这些关系并填充持有所有结果的JSON对象。您需要部署这些Cloud Code更新,然后从iOS端作为函数进行访问。结果将以包含帖子,标签,喜欢和评论的值的NSDictionary的形式返回。帖子是Post对象的数组。标签,喜欢和评论是NSDictionary对象,它有postId作为访问Parse对象数组的关键。
这样一个函数的调用会让你想要你的需要。
我已经在下面的代码中包含了一些代码作为参考,以防GitHub发生了什么变化。
// Helper functions in PT namespace
var PT = {
eachItem : function (items, callback) {
var index = 0;
var promise = new Parse.Promise();
var continueWhile = function(nextItemFunction, asyncFunction) {
var item = nextItemFunction();
if (item) {
asyncFunction(item).then(function() {
continueWhile(nextItemFunction, asyncFunction);
});
}
else {
promise.resolve();
}
};
var nextItem = function() {
if (index < items.length) {
var item = items[index];
index++;
return item;
}
else {
return null;
}
};
continueWhile(nextItem, callback);
return promise;
},
arrayContainsItem : function(array, item) {
// True if item is in array
var i = array.length;
while (i--) {
if (array[i] === item) {
return true;
}
}
return false;
},
arrayContainsOtherArray : function(array, otherArray) {
/// True if each item in other array is in array
var i = otherArray.length;
while (i--) {
if (!PT.arrayContainsItem(array, otherArray[i])) {
return false;
}
}
return true;
},
fetchPostTags : function(post) {
return post.relation("tags").query().find();
},
fetchPostLikes : function(post) {
return post.relation("likes").query().find();
},
fetchPostComments : function(post) {
var query = new Parse.Query(Comment);
query.include("owner");
query.equalTo("post", post);
return query.find();
},
fetchPostDetails : function(post, json) {
json.tags[post.id] = [];
json.likes[post.id] = [];
json.comments[post.id] = [];
return PT.fetchPostTags(post).then(function(tags) {
json.tags[post.id] = tags;
return PT.fetchPostLikes(post);
}).then(function(likes) {
json.likes[post.id] = likes;
return PT.fetchPostComments(post);
}).then(function(comments) {
json.comments[post.id] = comments;
json.count++;
return Parse.Promise.as();
});
},
};
喜欢'请解决我的问题'。 :D – Peres
问题是什么? –
在frndquery我得到pfrelation对象,而我从frnd查询获得的对象之后,我reterving从关系对象的数据,所以,现在我需要同时获得pfquery * frndquery对象我需要的关系对象的细节我不dont想要编写获取pfrelation对象的代码,我需要获取所有细节(即由于这个我可以减少加载的细节和关系细节)。所以请帮助我 – Romance