CloudCode promises in series, helper function gives error “Object has no method 'apply'”

I am new to JavaScript and I am trying to get my first bits of CloudCode working. I have created a helper function deleteFriendRequest as I will want this to be used by other functions. What I have written works but I get an error thrown.

My helper function:

function deleteFriendRequest(requestObjectId) {
    // create query to find FriendRequest from its objectId
    var requestQuery = new Parse.Query("FriendRequest");
    requestQuery.equalTo("objectId", requestObjectId);
    return requestQuery.first().then(function(friendRequestObj) {
        return friendRequestObj.destroy();
    });

}

A function where it is used:

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

    var currentUser = request.user;
    var friendRequestId = request.params.friendRequestObjectId;

    var fromUser;
    var fromRelation;

    var requestQuery = new Parse.Query("FriendRequest");
    requestQuery.equalTo("objectId", friendRequestId);
    requestQuery.include("fromUser");
    requestQuery.first().then(function(friendRequestObj) {
            fromUser = friendRequestObj.get("fromUser");
            fromRelation = currentUser.relation("friendsRelation");
            fromRelation.add(fromUser);
            return currentUser.save();
        }).then(function(friendRequestObj) {
            var toRelation = fromUser.relation("friendsRelation");
            toRelation.add(currentUser);
            return fromUser.save();
        }).then(deleteFriendRequest(friendRequestId))
        .then(function(error) {
            response.error(error);
        });

});

When I run createFriendship I get the following error:

Input: {"friendRequestObjectId":"qVIa9zzPR0"}
Result: TypeError: Object [object Object] has no method 'apply'
    at e (Parse.js:2:8941)
    at Parse.js:2:8390
    at Array.forEach (native)
    at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:661)
    at c.extend.resolve (Parse.js:2:8341)
    at Parse.js:2:9020
    at e (Parse.js:2:8941)
    at Parse.js:2:8390
    at Array.forEach (native)
    at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:661)

Thanks in advance for any help anyone can offer.

Answers


The expression ...

`.then(deleteFriendRequest(friendRequestId))` 

... calls deleteFriendRequest(friendRequestId) immediately and passes the returned promise to .then().

However, .then() accepts functions as its arguments, not promises.

Try :

.then(function() {
    deleteFriendRequest(friendRequestId);
})

or

.then(deleteFriendRequest.bind(null, friendRequestId))

Going further ...

Much of what is in deleteFriendRequest() is the derivation of friendRequestObj, which is a repetition of work already done in the caller function, so unless deleteFriendRequest() is needed elsewhere in the app, the line return friendRequestObj.destroy(); can be moved to the caller, and deleteFriendRequest() can disappear.

Parse.Cloud.define("createFriendship", function(request, response) {
    Parse.Cloud.useMasterKey();
    var requestQuery = new Parse.Query("FriendRequest");
    requestQuery.equalTo("objectId", request.params.friendRequestObjectId);
    requestQuery.include("fromUser");
    requestQuery.first()
    .then(function(friendRequestObj) {
        var fromUser = friendRequestObj.get("fromUser");
        request.user.relation("friendsRelation").add(fromUser);
        fromUser.relation("friendsRelation").add(request.user);

        // The two saves can most likely be performed in parallel
        return Parse.Promise.when(
            request.user.save(), 
            fromUser.save()
        )
        .then(function() {
            return friendRequestObj.destroy(); //<<<<<<<< moved here from deleteFriendRequest()
        });
    })
    .then(response.success, response.error);
});

Need Your Help

Which one is for video chat?

flash flash-media-server

I'm a bit confused about these 2. Every time I do a search about video chat, I see people talking about FMS, but I was under the impression you can only do 2-way video chat with FMIS. Is this true?

.NET Data Providers - How do I determine what they can do?

.net transactions dataprovider nested-transactions

I have code which could be executed using a Provider that doesn't support transactions, or doesn't support nested transactions.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.