I'm learning Parse, kind of stuck in one bug related to adding objects to a relation column (two related classes). following is the code. basically my organization class is related to users class by a relation field called admins. an organization can have many admins. It may not be optimized what I'm doing, so please guide me. I'm first checking existence of passed objectId of admin and organization. This function is not for general users so I can pass objectId. I'm not clear what's wrong here, but if I print contents of relation object before and after adding, className's value is null in after call. Please advise what am I missing or how I can improve the code and fix it :) . by the way, I've tried many solutions posted for couple of days, but none has helped so far, hence posting new question. Many Thanks.
Parse.Cloud.define("addAdminToOrganization", function(request, response)
{
var queryAdmin = new Parse.Query("Users");
var reqOrgId = request.params.organizationId;
var reqAdminId = request.params.adminId;
queryAdmin.equalTo("objectId", reqAdminId);
queryAdmin.find(
{
success: function(fetchedAdmin)
{
var reqOrgId = request.params.organizationId;
var queryOrg = new Parse.Query("Organizations");
queryOrg.equalTo("objectId",reqOrgId);
var fetchedAdm = fetchedAdmin;
queryOrg.get(reqOrgId,
{
success: function(fetchOrg)
{
var _fetchOrg = fetchOrg;
var adminRelation = fetchOrg.relation("admins");
Parse.Cloud.useMasterKey();
var reqAdmin = request.params.adminId;
adminRelation.add(reqAdmin);
_fetchOrg.save(null,
{
success:function() {
console.log("Admin added to Organization");
response.success("Admin:" + reqAdminID + " added to Organization:"+reqOrgId);
},
error:function(err) {
console.log("Error while adding admin to Organization, "+ err.message);
response.error("Unable to add Admin");
}
} );
},
error: function(err){
console.log("Organization not found : " + err.code);
response.error("Organization not found.");
}
});
},
error:function(err){
console.log("Admin not found: " + err.code);
response.error("Admin not found");
}
});
});