0

I have a pointer relation between two classes and I need to fetch class A thought class B. It works fine when I use Rest and Android SDK, but I'm constructing a function in CloudCode and it needs to work there.

Tasks (Point to) ExtraTasks

I need to get all ExtraTasks, that a given Task points to.

Any ideas? tks

  • Pointer key: taskExtraTasks
  • "Parent" Class: Tasks
  • "Child" Class: ExtraTasks

Class Tasks and it's relation Tasks class

Inside the taskExtraTask relation enter image description here

JSON that constructed the Pointer(Relation?). The relation was constructed via REST api.

{"taskExtraTasks": 
  {
    "__op":"AddRelation",
    "objects":
        [
            {
                "__type":"Pointer",
                "className":"Tasks",
                "objectId":"TASK_ID"
            }
        ] 
  } 
}

CloudCode to get the relation. (Considering the suggestion by JackalopeZero)

Parse.Cloud.define("extras", function(request, response) {
  var taskId = "0n5svlATjG";
  var taskQuery = new Parse.Query("Tasks");
  taskQuery.get(taskId).then(function(task){
    var taskRelationQuery = task.relation("taskExtraTasks");
    taskRelationQuery.equalTo("taskExtraTasks", task);
    return taskRelationQuery.find();
  })
  .then(function(extraTasks){
    // your extra tasks are now accessible here
    response.success( extraTasks );
  },function(error){
    response.error( error );
  });
});

Response:

{
  "code": 141,
  "error": "{\"code\":101,\"message\":\"Object not found.\"}"
 }

NOTE: The answer below is correct, although my problem was a bit more complex. I was mistaken Relation and Pointer and couldn't get my head around it, thanks to the lack of information in the REST documentation. If you are stuck in the same way I was, I recommend the creation of Pointer and Relations using Parse UI. After that you can use the REST to deal with those. After you create the Pointer column using the UI, you can add a point via REST using a JSON obj:

// Pointer CLASS_A -> CLASS_B
// Method: "PUT" , path: classes/CLASS_B/CLASS_B_OBJECT_ID
{"pointerName": 
{
    "__type"    :"Pointer",
    "className" :"CLASS_A",
    "objectId"  :"CLASS_A_OBJECT_ID"
}    

}

1 Answer 1

1

You dont need to extend if you are simply querying. You can change:

var ExtraTasks  = Parse.Object.extend("ExtraTasks");
        var qExtra = new Parse.Query(ExtraTasks);

to:

var qExtra = new Parse.Query("ExtraTasks");

Also, just make sure that if you are querying using pointers in Parse, you need to make sure that you query using a Parse object, not just an ID.

For example, if the task column is type pointer, this will not work:

var taskId = "13jkasd";
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("task", taskId);
return userQuery.find();

You would need to use:

var taskId = "13jkasd";
var taskQuery = new Parse.Query("Task");
taskQuery.get(taskId).then(function(task){
    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo("task", task);
    return userQuery.find();
})

One last thing that may be the problem:

Your code seems to point out that there is a one to many relationship between task and extraTask

qExtra.equalTo("taskExtraTasks", task );

If you are querying a relation, then it needs to be done slightly differently. Think of a relation as a join table that doesnt appear on your interface, you need to query that join table.

var taskId = "0n5svlATjG";
var taskQuery = new Parse.Query("Task");
taskQuery.get(taskId).then(function(task){
    var taskRelationQuery = task.relation("taskExtraTasks");
    taskRelationQuery.equalTo("task", task);
    return taskRelationQuery.find();
})
.then(function(extraTasks){
    // your extra tasks are now accessible here
});

For more information on relations, check out the Parse JS developers guide.

BUT...

The problem here seems to stem from the code in your comment. You are adding a pointer within a relation. You dont need to touch relations if you have a one to many relationship, you just need to use a pointer column in your Task table. Just create the new extraTask with a pointer to task. That way you will be able to use my original answer in your code (but more to the point, that would be the correct way to create this relationship)

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the answer friend. By my understanding is a Relation of the type Pointer. I used an Json like this: {"__op":"AddRelation","objects":[{"__type":"Pointer","className":"Tasks","objectId":"TASK_ID"}]}
Ok I've added that to the answer
I tried to apply your answer as I understood, but still got an error. { "code": 141, "error": "{\"code\":101,\"message\":\"Object not found.\"}" }. I'm updating the question with more details. It would be great if you could help a little more. tks
Your code does not match mine. You are using taskRelationQuery.equalTo("taskExtraTasks", task); instead of taskRelationQuery.equalTo("task", task); but I need to see the other columns in taskExtraTask relation to confirm this
I understood that the taskRelationQuery.equalTo("task", task); was related to the relation key name, hence 'taskExtraTasks'. I'm adding a new picture with the columns names.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.