从循环中获取对象Parse

时间:2015-10-20 10:12:01

标签: javascript parse-platform cloud

在Parse服务器功能中,它获取了匹配和配置文件。 从查询到获取匹配,调用另一个函数来按ID获取配置文件,但结果是:

{"_resolved":false,"_rejected":false,"_reso  resolvedCallbacks":[],"_rejectedCallbacks":[]}

主要查询:

mainQuery.find().then(function(matches) {
_.each(matches, function(match) {
        // Clear the current users profile, no need to return that over the network, and clean the Profile


        if(match.get('uid1') === user.id) {
            match.set('profile2', _processProfile(match.get('profile2')))
            match.unset('profile1')
        }
        else if (match.get('uid2') === user.id) {
             var profileMatch = _getProfile(match.get('profile1').id);
             alert(">>>"+JSON.stringify(profileMatch));

            match.set('profile1', _processProfile(match.get('profile1')))
            match.unset('profile2')
        }

})

获取个人资料信息的功能:

function _getProfile(id){

var promise = new Parse.Promise();



    Parse.Cloud.useMasterKey();

    var queryProfile = new Parse.Query(Profile);

        return queryProfile.equalTo("objectId",id).find()
        .then(function(result){

            if(result){
                promise.resolve(result);
                alert("!!!!"+result);
            }

                else {
            console.log("Profile ID: " + id + " was not found");
            promise.resolve(null);

            }

        },
        function(error){

            promise.reject(error)
        });
        return promise;     
}

1 个答案:

答案 0 :(得分:1)

刚才发现这有点晚了。你可能已经开始了,但是对于未来的读者来说:解决这类问题的关键是使用promises作为来自小型逻辑异步(或者有时是asynch,如你的情况)操作的回报。

整个_getProfile函数可以重写为:

new DateTime(2015, 1, 1, 0, 0, totalSeconds)

因为它返回了一个承诺,你不能这样称呼它:

function _getProfile(id){
    Parse.Cloud.useMasterKey();
    var queryProfile = new Parse.Query(Profile);
    return queryProfile.get(id);
}

相反,请这样称呼:

var myProfileObject = _getProfile("abc123");
// use result here

知道这一点,我们需要重新调整调用此函数的循环。关键的想法是,由于循环有时产生了承诺,我们需要让这些承诺在最后得到解决。

_getProfile("abc123").then(function(myProfileObject) { // use result here });