I have a PFUser
that has a days
relation to a Day
PFObject
.
In my database it looks like this:
{
"_id": "WjLAnMuRmp",
"name": "c",
"_created_at": {
"$date": "2016-08-04T15:28:51.759Z"
},
"_updated_at": {
"$date": "2016-08-24T19:44:44.774Z"
},
"days": [
{
"__type": "Pointer",
"className": "Day",
"objectId": "BrQwmKAbJC"
},
{
"__type": "Pointer",
"className": "Day",
"objectId": "6wuDMl4kKI"
}
]
}
Pretty straight forward. In my cloud code, I'm trying to send up a PFUser
objectId
, then fetch all the days
they have and iterate over them. For some strange reason I keep getting 0 returned when I do a relation query.
Here is what I'm working with:
Parse.Cloud.define("getDayAveragesForUser", function(request, response) {
console.log("-getDayAveragesForUser");
// Create the query on the User class
var fetchedUser = new Parse.User({id:request.params.userObjectId});
console.log("the fetched user: " + fetchedUser.id);
var relation = fetchedUser.relation("days");
var query = relation.query();
query.limit(365);
query.ascending("createdAt");
query.find({
success: function(results) {
console.log("Successfully retrieved " + results.length + " Days.");
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
}
});
});
When I print the fetchedUser.id
it's correct so I know I'm on the right user. This seems based on the documentation example:
var relation = user.relation("likes");
relation.query().find({
success: function(list) {
// list contains the posts that the current user likes.
}
});
This should be working fine.
===
I just add this to my Cloud Code to test:
var days = fetchedUser.get("days");
console.log("type of: " + typeof days);
which from this I get:
type of: undefined
The reason is that your days are not saved as relations but as a pointers. And in parse relations and pointers are handled in different ways. In order to fetch the days pointer you need to change your query to look like the following:
var userQuery = new Parse.Query(Parse.User);
userQuery.include("days"); // include the days pointer in the results
userQuery.get(request.params.userObjectId, {
success: function(user) {
// This function will *not* be called.
console.log(user.get("days")); // print the days to console
},
error: function(error) {
}
});