parse-platform

Query Parse Relation with Object ID Only


I want to conduct a Parse query on a class that has a relation column. From what I understand, I can't pass the entire relation object with a cloud function, I can only pass the objectId as a string.

So in my iOS app, I pass the objectId like this:

PFCloud.callFunctionInBackground("customReport", withParameters: ["thing":thing.objectId]){ (response, error) -> Void in
  println(response)
}

Then in my Cloud code function:

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) 
  }

  //...

})

But this doesn't work because the equalTo statement expects a thing object, not an objectId.

Is there a way I can query a relation with just the objectId? Or do I have to query for the thing object using the objectId and then do the equalTo check?


Solution

  • It looks like you can query by objectId after all. You just need to create an empty object with the ID first like this:

    //In my Cloud Code function...    
    var ThingObject = Parse.Object.extend("Thing")
    var temporaryThing = new ThingObject()
    
    temporaryThing.id = request.params.thing
    query.equalTo('thing',temporaryThing)