Reputation: 801
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
Inside the taskExtraTask relation
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"
}
}
Upvotes: 0
Views: 143
Reputation: 6394
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)
Upvotes: 1