S N
S N

Reputation: 600

Parse cloud code unable to add relation

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");
            }
    });
});

Upvotes: 1

Views: 875

Answers (1)

Timothy Walters
Timothy Walters

Reputation: 16874

There's a couple of issues with your code:

queryAdmin.equalTo("objectId", reqAdminId);
queryAdmin.find( 
{
    success: function(fetchedAdmin)
    {

Because you did a find() the fetchedAdmin variable will be an array with 0 or more elements in it, if you know you just want a single record drop the equalTo() and just use get().

Strangely enough you don't seem to be actually using fetchedAdmin or the variable fetchedAdm that you copy it into.

queryOrg.equalTo("objectId",reqOrgId);
queryOrg.get(reqOrgId, 

The equalTo() call is redundant since you have correctly used get() for this query.

var reqAdmin = request.params.adminId;
adminRelation.add(reqAdmin);

Here you are reading a string ID and trying to add it to a relation, you need to create a pointer, or use the admin object you retrieved (if you actually had gotten one properly).

A simplification of your code would be as follows:

Parse.Cloud.define("addAdminToOrganization", function(request, response)
{
    Parse.Cloud.useMasterKey();
    var reqOrgId = request.params.organizationId;
    var reqAdminId = request.params.adminId;

    var queryAdmin = new Parse.Query("Users");
    queryAdmin.get(reqAdminId, 
    {
        success: function(fetchedAdmin)
        {
            var queryOrg = new Parse.Query("Organizations");
            queryOrg.get(reqOrgId, 
            {
                success: function(fetchedOrg)
                {
                    var adminRelation = fetchedOrg.relation("admins");
                    adminRelation.add(fetchedAdmin);
                    fetchedOrg.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");
            }
    });
});

Upvotes: 4

Related Questions