解析:Query.find在循环内找不到数据(即使使用了promises)

时间:2015-12-05 20:27:50

标签: javascript parse-platform promise cloud-code

我实际上传递了两个列表(existingLeaderboard和newResultSetObj),我正在根据一些逻辑比较和查询它们。在最后我想根据逻辑向每个用户发送通知。但不知怎的,我的代码不等待查询查询(控制台日志永远不会出现在日志中)。这是代码 -

function compareAndSendNotification(existingLeaderboard, newResultSetObj) {
        var _ = require('underscore');
        var leadboardarray=[];
        console.log("New leaderboard" + newResultSetObj);
        console.log("Existing leaderboard" + existingLeaderboard);
        leadboardarray = existingLeaderboard;


        if (typeof existingLeaderboard == "undefined" || typeof newResultSetObj == "undefined"
            || existingLeaderboard.length == 0 || newResultSetObj.length == 0) {
            console.log("existingLeaderboard or newResultSetObj is null");
            return true;
        }

        var getUserFromLookPromise = Parse.Promise.as();

            var fromUser = "";
            //Event out looks


            _.each(existingLeaderboard, function(test) {
                if (contains(newResultSetObj, test)) {
                    console.log("Look id still in top 3 i =" + test);
                    // get user from look
                    var lookId = test;
                    console.log('get look id called ' + lookId);

                    var look = Parse.Object.extend("Look");
                    var lookQuery = new Parse.Query(look);
                    lookQuery.equalTo("objectId", lookId);

                    console.log("before find query");

                    //getUserFromLookPromise=getUserFromLookPromise.then(function(){
                    //     return lookQuery.find();
                    lookQuery.find().then(function(){
// following console log never prints 
                        console.log("inside find query");
                        var lookqueryPromise = Parse.Promise.as();
                        lookqueryPromise = lookqueryPromise.then(function() {
                            return lookQuery.find();
                        });
                        //while(lookqueryPromise.find().length==0){
                        //    lookqueryPromise.resolve(lookQuery.find());
                        //}
                        return lookqueryPromise;

                    }).then(function (notificationUser) {
                        console.log("inside resolve user");
                        if ( typeof notificationUser == "undefined" || notificationUser.length == 0 ) {
                            getUserFromLookPromise.resolve(notificationUser);
                        }
                        fromUser = notificationUser[0].get['fromUser'];
                        return fromUser;
                    }).then(function (user) {
                        console.log("sending notification to user " + user);
                        sendNotification(user, "hello");
                    });


                }
            });

        return getUserFromLookPromise;

    }

我是新来解析的。请让我知道我做错了什么。

1 个答案:

答案 0 :(得分:1)

让我们从一个较小的问题开始:给出一个“Look”对象给出它的id:

function lookWithId(objectId) {
    var query = new Parse.Query("Look");
    query.include("fromUser");  // presumes "fromUser" is a pointer to Parse.User
    return query.get(objectId);
}

现在,让我们使用该函数来解决一个更大的问题:在给定一组id的情况下获取一组“Look”对象:

var _ = require('underscore');

function looksWithIds(objectIds) {
    var promises = _.map(objectIds, function(objectId) {
        return lookWithId(objectId);
    });
    return Parse.Promise.when(promises);
}

现在更加强硬。给定一组用户,向每个用户发送一条消息。只有当您的应用程序通过在每次安装时设置“用户”指针将安装与用户关联时,才能执行此操作。

function sendMessageToUser(user) {
    var query = new Parse.Query(Parse.Installation);
    query.equalTo('user', user);
    return Parse.Push.send(where: query, data: { alert: "Hello" });
}

并像之前那样复数那个......

function sendMessagesToUsers(users) {
    var promises = _.map(users, function(user) {
        return sendMessageToUser(user);
    });
    return Parse.Promise.when(promises);
}

您编写的另一个函数:给定两个排行榜,返回符合某些应用标准的“Look”对象ID数组...

function lookIdsFromLeaderBoards(leaderBoardA, leaderBoardB) {
  // written by @user2922400, based on the app
  // no promises needed here, just answer an array of Look object ids
}

现在,您尝试的功能就像馅饼一样简单:

function sendMessagesToLeader(leaderBoardA, leaderBoardB) {
    var lookIds = lookIdsFromLeaderBoards(leaderBoardA, leaderBoardB);
    return looksWithIds(lookIds).then(function(lookObjects) {
        var users = _.map(lookObjects, function(lookObject) {
            return lookObject.get("fromUser");
        });
        return sendMessagesToUsers(users);
    })
}