找到你要的答案

Q:Query Inside Parse Cloud For Loop

Q:循环解析云查询

I have been trying to run my Parse Cloud Code for some time and can not seem to get around this problem:

I have an array of Parse objectId's called IDArray. I then am sending the array as a parameter of a PFCloud call. Once the array has been sent to the Cloud Code, I can not seem to successfully create a for loop that goes through and updates a number value stored as "points" on Parse for each objectId.

In a nutshell, this is all I am trying to accomplish:

  • I just need to be able to have the for loop go through each objectId and perform an action for each ID.

I have been trying to get this to work for some time but have had no luck. Here is the code that I have been trying to manipulate - hopefully it will give someone a starting point to answer my question.

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;

                   
    for (var i = 0; i < list.length; i++) {
                   
        var userdata = list[i];        
        query.get(userdata, {
                                       
            success: function(UserData) {
                               
                response.success('Should add up');
                UserData.addUnique('Done', +1);
                UserData.save();
            },
            error: function() {
                response.error('something went wrong' );
            }
        });
    }
});

If someone could please help me with this I would be very grateful. Thank you

我一直试图运行我的解析云代码了一段时间,似乎不能绕过这个问题:

我有一个数组解析ObjectId叫idarray。我是把数组作为一个pfcloud调用参数。一旦阵列已经发送到云的代码,我似乎无法成功创建一个循环,通过并更新存储为“解析每个ObjectId点”的数值。

简而言之,这就是我想要完成的一切:

  • I just need to be able to have the for loop go through each objectId and perform an action for each ID.

我一直试图让这个工作了一段时间,但没有运气。这里是我一直试图操纵的代码-希望它会给人一个起点来回答我的问题。

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;

                   
    for (var i = 0; i < list.length; i++) {
                   
        var userdata = list[i];        
        query.get(userdata, {
                                       
            success: function(UserData) {
                               
                response.success('Should add up');
                UserData.addUnique('Done', +1);
                UserData.save();
            },
            error: function() {
                response.error('something went wrong' );
            }
        });
    }
});

如果有人能帮助我,我会非常感激。谢谢你

answer1: 回答1:

I think the problem is you are sending response multiple times, you should wait for all the promises to finish and then send a response:

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;

    function checkUserData(userdata){   // returns parse promise for a particular userdata
        return query.get(userdata).then(function(){
            UserData.addUnique('Done', +1);
            UserData.save();            
        });
    }

    Parse.Promise.when(list.map(checkUserData)) // mapping all the elements in the list to resp promises
        .then(function(){   // on success
            response.success('Should add up');
        }).catch(function(e){    // on failure
             response.error('something went wrong' );
        });
});

Edit: if for some reason map is not available( in case of older browsers or list not being an normal javascript array), you can do something like:

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;
    var promises = [];
    function checkUserData(userdata){   // returns parse promise for a particular userdata
        return query.get(userdata).then(function(){
            UserData.addUnique('Done', +1);
            UserData.save();            
        });
    }

    for(var i=0;i<list.length;i++){
        promises.push(checkUserData(list[i]));
    }


    Parse.Promise.when(promises) // once all the promises are resolved...
        .then(function(){   // on success
            response.success('Should add up');
        }).catch(function(e){    // on failure
             response.error('something went wrong' );
        });
});

我认为问题是你发送的响应多次,你应该等待所有的承诺完成,然后发送响应:

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;

    function checkUserData(userdata){   // returns parse promise for a particular userdata
        return query.get(userdata).then(function(){
            UserData.addUnique('Done', +1);
            UserData.save();            
        });
    }

    Parse.Promise.when(list.map(checkUserData)) // mapping all the elements in the list to resp promises
        .then(function(){   // on success
            response.success('Should add up');
        }).catch(function(e){    // on failure
             response.error('something went wrong' );
        });
});

编辑:如果由于某种原因,地图不可用(如旧的浏览器或列表不是一个正常的JavaScript数组),你可以做这样的事情:

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;
    var promises = [];
    function checkUserData(userdata){   // returns parse promise for a particular userdata
        return query.get(userdata).then(function(){
            UserData.addUnique('Done', +1);
            UserData.save();            
        });
    }

    for(var i=0;i<list.length;i++){
        promises.push(checkUserData(list[i]));
    }


    Parse.Promise.when(promises) // once all the promises are resolved...
        .then(function(){   // on success
            response.success('Should add up');
        }).catch(function(e){    // on failure
             response.error('something went wrong' );
        });
});
javascript  jquery  parse.com  promise  cloud-code