I have a class with multiple pointers, which I want to return their full objects in the response (like a join in SQL). The example below does that, but it returns JSON instead of a Parse object. How do I return an array of objects which contain each Parse object from the pointers WITHOUT doing an additional query for each pointer?
query.find().then(function(results){
/* Go Through Each Comment*/
var commentsArray = new Array();
for(i in results){
/* Set obj to current comment*/
var obj = results[i];
/* Get Post's Name */
var postName = obj.get("post").get("postName");
/* Get Post's Message */
var postMsg = obj.get("post").get("postMsg");
/* Get Post Author's Name */
var authorName = obj.get("post").get("postAuthor").get("name");
/* Get Comment's Message */
var commentMsg = obj.get("msg");
/* Get Comment's Author*/
var commentAuthor = obj.get("user").get("name");
/* Let's Put the Comment Information in an Array as an Object*/
commentsArray.push({
post:{
name: postName,
msg: postMsg
},
author: authorName,
comment: {
author: commentAuthor,
msg: commentMsg
}
});
}
})
Edited (I'm building with Swift on the client):
var query = new Parse.Query("Profile");
query.equalTo("objectId", objectId);
query.find().then(function(profile) {
response.success(profile) // Returns profile parse object
}, function(error) {
response.error(error);
});
//RETURNS
"<Profile: 0x153f96570, objectId: HKdukNBlmA, localId: (null)> {\n}"
Parse.Cloud.define("getProfiles", function(request, response) {
var query = new Parse.Query("Profile");
query.include("friendId");
query.find().then(function(profiles) {
var res = [];
profiles.forEach(function(profile) {
var obj = {
firstName: profile.get("firstName"),
lastName: profile.get("lastName"),
age: profile.get("age"),
gender: profile.get("gender")
};
obj.friend = {
firstName: profile.get("friendId").get("firstName"),
lastName: profile.get("friendId").get("lastName"),
};
res.push(obj);
});
response.success(res);
}, function(error) {
response.error("Error: " + error.code + " " + error.message);
});
});
// RETURNS
[{
firstName: "bob",
lastName: "smith",
age: 19,
gender: male
friend: {
firstName: "tim",
lastName: "wang",
}
},
{
firstName: "chris",
lastName: "scalia",
age: 24,
gender: male
friend: {
firstName: "ben",
lastName: "calder",
}
}]
I prefer the former.
var postName = obj.get("post").get("postName");gives you a nice string inpostName, and not an error, then your code works. You have the post object and don't need another fetch.