我是JavaScript的新手,我正在尝试使CloudCode的第一部分工作。我创建了一个辅助函数deleteFriendRequest,因为我希望其他函数可以使用它。我写的作品行得通,但出现错误。

我的助手功能:

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

}


使用它的函数:

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

});


当我运行createFriendship时,出现以下错误:

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)


在此先感谢您提供任何帮助。

最佳答案

表达方式 ...

`.then(deleteFriendRequest(friendRequestId))`


...立即调用deleteFriendRequest(friendRequestId)并将返回的承诺传递给.then()

但是,.then()接受函数作为其参数,而不是promise。

尝试:

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


要么

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


进一步...

deleteFriendRequest()中的大部分内容是friendRequestObj的派生,这是调用方函数中已经完成的工作的重复,因此,除非应用中其他地方需要deleteFriendRequest(),否则行return friendRequestObj.destroy();可以移至呼叫者和deleteFriendRequest()可以消失。

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

关于javascript - CloudCode promise ,助手功能会给出错误“对象没有方法'应用'”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32073705/

10-12 15:42