我想对具有关系列的类进行解析查询。据我了解,我无法使用云函数传递整个关系对象,我只能将
objectId
作为字符串传递。
所以在我的 iOS 应用程序中,我像这样传递
objectId
:
PFCloud.callFunctionInBackground("customReport", withParameters: ["thing":thing.objectId]){ (response, error) -> Void in
println(response)
}
然后在我的云代码功能中:
Parse.Cloud.define("customReport", function(request, response) {
var promises = []
var query = new Parse.Query("MyClass")
//Search for thing
if(request.params.thing != ""){
query.equalTo('thing',request.params.thing)
var promise1 = query.find()
promises.push(promise1)
}
//...
})
但这不起作用,因为
equalTo
语句需要一个 thing
对象,而不是 objectId
。
有没有办法只用
objectId
来查询关系? 或者我是否必须使用 thing
来查询 objectId
对象,然后 then 进行 equalTo
检查?
看起来你毕竟可以通过
objectId
查询。你只需要先创建一个带有 ID 的空对象,如下所示:
//In my Cloud Code function...
var ThingObject = Parse.Object.extend("Thing")
var temporaryThing = new ThingObject()
temporaryThing.id = request.params.thing
query.equalTo('thing',temporaryThing)
你是对的,你不能直接传递关系对象。你可以做的就是在关系列中传递你要搜索的对象的objectId,然后查询关系列等于objectId的类。
这里是一个示例,如果您在 _User 类上有一个名为“friends”的关系列,并且您在 iOS 应用程序中传递 _User 对象的 objectId:
Parse.Cloud.define("getFriends", function(request, response) {
var userId = request.params.userId;
var query = new Parse.Query(Parse.User);
query.equalTo("friends", userId);
query.find({
success: function(friends) {
response.success(friends);
},
error: function(error) {
response.error(error);
}
});
});
您必须将 id 更改为指针,然后可以将其传递给 query.whereEqualTo。 https://parse.com/questions/updating-a-field-without-retriving-the-object-first
var Point = Parse.Object.extend("Event");
var point = new Point();
point.id = "dlkj83d";
你绝对可以使用includeKey...
基本上,当它获取具有指向它的指针的对象时,
myQuery.includeKey("columnName")
将获取该键的整个对象。 2换1!
更多:
http://blog.parse.com/announcements/queries-for-relational-data/