问题
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 eachobjectId
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
回答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' );
});
});
来源:https://stackoverflow.com/questions/32000222/query-inside-parse-cloud-for-loop