在内部解析云中查询循环 [英] Query Inside Parse Cloud For Loop

查看:99
本文介绍了在内部解析云中查询循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试运行我的Parse Cloud代码,但似乎无法解决此问题:

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

我有一个解析objectId的数组,称为 IDArray .然后,我将数组作为PFCloud调用的参数发送.将数组发送到Cloud Code后,我 似乎无法成功创建一个for loop,该c经历并为 each 在Parse上更新存储为点"的数字值/em> objectId.

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:

  • 我只需要能够通过每个for loop objectId并为每个ID执行一个操作.
  • 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

推荐答案

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

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' );
        });
});

:如果由于某些原因map不可用(在较旧的浏览器或list不是普通的javascript数组的情况下),您可以执行以下操作:

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' );
        });
});

这篇关于在内部解析云中查询循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆