解析嵌套的承诺?

时间:2014-09-08 23:09:29

标签: node.js parse-platform cloud-code

我正在尝试查找电话号码与输入数组中的电话号码相匹配的用户。但是,我相信在userQuery完成之前会调用response.success(friends);。使用我当前的数据,这应该返回2个用户,但是,因为它当前写入它只返回一个空数组。如果我将成功响应放在userQuery中(只是为了测试),我会收到一个用户,所以我知道查询是正确的。我需要另一个嵌套的承诺吗?

Parse.Cloud.define("addFriends", function(request, response) {
    var _ = require('underscore.js');
    Parse.Cloud.useMasterKey();

    var friends=[];

    var theNumbers = request.params.phoneNumbers;
    var userId = request.params.user;


    var currentUserQuery = new Parse.Query("User");
    currentUserQuery.equalTo("objectId", userId)
    currentUserQuery.first().then(function(currentUser) {
        var promise = Parse.Promise.as();


        _.each(theNumbers, function(aNumber) {
            promise = promise.then(function() {
                console.log('looking for: ' + aNumber);

                userQuery = new Parse.Query("User");
                userQuery.equalTo("phoneNumber", aNumber);
                userQuery.first().then(function(foundUser){
                    console.log("Found user: " + foundUser);

                    friends.push(foundUser);
                });
            });

            return promise;
        });
    }).then(function(){
        console.log('done');
        response.success(friends);
    });
});

2 个答案:

答案 0 :(得分:1)

将所有子承诺添加到数组朋友中,使用Parse.Promise.when返回新承诺。 response.success需要返回arguments因为Parse.Promise.when每个参数返回一个结果而不是数组。

Parse.Cloud.define("addFriends", function(request, response) {
    var _ = require('underscore.js');
    Parse.Cloud.useMasterKey();

    var friends=[];

    var theNumbers = request.params.phoneNumbers;
    var userId = request.params.user;


    var currentUserQuery = new Parse.Query("User");
    currentUserQuery.equalTo("objectId", userId)
    currentUserQuery.first().then(function(currentUser) {


        _.each(theNumbers, function(aNumber) {
            console.log('looking for: ' + aNumber);

            userQuery = new Parse.Query("User");
            userQuery.equalTo("phoneNumber", aNumber);
            friends.push(userQuery.first());
        });
        return Parse.Promise.when(friends);
    }).then(function(){
        console.log('done');
        response.success(arguments);
    });
});

答案 1 :(得分:0)

您需要返回承诺才能使下一个.then来电等待,直到问题解决为止。

Parse.Cloud.define("addFriends", function(request, response) {
    var _ = require('underscore.js');
    Parse.Cloud.useMasterKey();

    var friends=[];

    var theNumbers = request.params.phoneNumbers;
    var userId = request.params.user;


    var currentUserQuery = new Parse.Query("User");
    currentUserQuery.equalTo("objectId", userId)
    currentUserQuery.first().then(function(currentUser) {
        var promise = Parse.Promise.as();


        _.each(theNumbers, function(aNumber) {
            promise = promise.then(function() {
                console.log('looking for: ' + aNumber);

                userQuery = new Parse.Query("User");
                userQuery.equalTo("phoneNumber", aNumber);
                userQuery.first().then(function(foundUser){
                    console.log("Found user: " + foundUser);

                    friends.push(foundUser);
                });
            });

            return promise;
        });

        // This is where you return the promise, to delay the call to response.success
        return promise;

    }).then(function(){
        console.log('done');
        response.success(friends);
    });
});