1

我正在尝试查找电话号码与输入数组中的电话号码匹配的用户。但是,我相信response.success(friends);在 userQuery 完成之前被调用。使用我当前的数据,这应该返回 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);
    });
});
4

2 回答 2

1

将所有子承诺添加到数组朋友中,返回一个新的承诺Parse.Promise.whenresponse.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);
    });
});
于 2014-09-09T01:59:52.010 回答
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);
    });
});
于 2014-09-09T01:41:18.747 回答