0
votes

I am trying to write a Cloud Code function that will allow me to edit the data of another user as I cannot do that in the application it self. What the code does (I should say tries to do as I don't know JS) is fetch a User object and a Group (a class I created) object using two separate queries based on the two object IDs inputed. Here is my code

Parse.Cloud.define("addInvite", function(request, response) { 
Parse.Cloud.useMasterKey();

var userID = request.params.user;
var groupID = request.params.group;

var user;
var group;


var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("objectId", userID);


return userQuery.first
({
    success: function(userRetrieved)
    {
        user = userRetrieved;    
    },
    error: function(error)
    {
        response.error(error.message);
    }
});



var groupObject = Parse.Object.extend("Group");
var groupQuery = new Parse.Query(groupObject);
groupQuery.equalTo("objectId", groupID);


return groupQuery.first
({
    success: function(groupRetrieved)
    {
        group = groupRetrieved;    
    },
    error: function(error)
    {
        response.error(error.message);
    }
});
var relations = user.relation("invited");
relations.add(group);

user.save();
response.success(); 

});

Every time I execute the method I get the error: [Error]: success/error was not called (Code: 141, Version: 1.9.0)

Can anyone help with this? Thanks.

1
Remove the returns, you don't need them - kRiZ

1 Answers

2
votes

Every function in Parse Cloud returns a Promise. This also includes any query functions which you run to retrieve some data. Clearly in your code you are returning a Promise when you execute a query which abruptly ends your cloud function when your query completes. As you do not call a response.success() or response.error() in any of the success blocks, your cloud function returns without setting a suitable response, something that Parse requires and hence the error. Your code needs to chain all the promises to ensure your code is executed correctly and return success/error in the last step:

Parse.Cloud.define("addInvite", function(request, response) { 

    Parse.Cloud.useMasterKey();

    var userID = request.params.user;
    var groupID = request.params.group;

    var user;
    var group;

    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo("objectId", userID);

    userQuery.first().then(function(userRetrieved) {
        user = userRetrieved;     
        var groupObject = Parse.Object.extend("Group");
        var groupQuery = new Parse.Query(groupObject);
        groupQuery.equalTo("objectId", groupID);
        return groupQuery.first();

    }).then( function(groupRetrieved) {
        //group = groupRetrieved;    
        var relations = user.relation("invited");
        relations.add(groupRetrieved);
        return user.save();

    }).then(function() {
        response.success(); 

    }, function(error) {
        response.error(error.message);
    });

});