I'm trying to execute the following in cloud code:
Parse.Cloud.job("fixUserStuffs", function(request, status) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.limit(1000);
query.each(function(user) {
user.set('password', 'abc');
return user.save();
}).then(function() {
status.success("completed successfully.");
}, function(error) {
status.error(error);
});
but it's returning me the error 'Failed with: Cannot iterate on a query with sort, skip, or limit.' This seems kind of pointless to me since I either need to skip or increase the number of objects returned rather than the default 100.
When you use Parse.Query.each(), it will process ALL records. 1, 1000, 1000000. It doesn't matter.
If you don't want to process all records, then you don't use each.
Related
I'm still learning how to use Parse and I'm working with migrating a subset of a database with 49 items. I read that Parse saveAll should be able to handle batches of 70-100 with no issues but when ever I run the code below, Parse always only saves 20 items.
I run a query to draw out all items with .each(), then I create new parse Objects and insert them into an array.
Once the each() finishes, the array contains all 49 elements but when saveAll runs, only 20 items are saved.
I read thru promises but I'm not certain how they'll work with saveAll, additionally, most people here don't seem to be having any issues with saveAll.
I've tested the code below as a background and cloud function.
Parse.Cloud.job("testEach2", function(request, response) {
var eachQuery = new Parse.Query("test_csv");
var Item = Parse.Object.extend("Item");
var toSave = [];
eachQuery.each(
function(result) {
var item = new Item();
item.set("name",result.get("v_products_name_1"))
console.log(item.name)
toSave.push(item)
}, {success: function(result) {
response.success();
console.log(toSave.length);
Parse.Object.saveAll(toSave).then(function(results){
console.log("Objects were saved!");
response.success("Awesome");
},function(error){
console.log(error);
response.error(error);
});
}, error: function() {} });
});
You should call response.success(); only after Parse.Object.saveAll() is finished. Remove the first response.success(); from your success callback.
I am using Cloud Code to update all users, everyday. It used to work, but now getting error after 5 minute processing. "the service is currently unavailable" without any reason. I have checked status.parse.com and there is no relevant down. I have 10 000 users.
Parse.Cloud.job("makeUsersPassiveAndSendPushes", function(request, status) {
Parse.Cloud.useMasterKey();
var activeUsers = [];
var limitDoneUsers = [];
var nowDate=new Date();
var updatedUsers = [];
var query = new Parse.Query(Parse.User);
query.equalTo("passive",false);
query.each(function(user) {
if(user.get("passive") === false){
activeUsers.push(user);
user.set("passive", true);
user.set("passiveDate",nowDate);
}
if(user.get("isLimitDone")){
limitDoneUsers.push(user);
}
user.set("isLimitDone",false);
user.set("activeMatch",null);
user.set("canGetMatch",true);
user.set("dailyMatchEndCount",0);
//user.set("lastMatchLimit",false);
user.set("todaysMatches",[]);
updatedUsers.push(user);
return user.save();
})
Could you help me? Thanks.
You may want to try modifying the last line from:
return user.save();
to use callbacks for the save function, to ensure they are firing in sequence:
return user.save(null, {
success: function (user) {
return user;
},
error: function (error) {
return Parse.Promise.error(new Error("error"));
}
});
Another alternative would be to use the saveAll function like this:
return Parse.Object.saveAll(updatedUsers).then(function() {
//code that fires after all objects are saved
});
Also, are you using the hosted Parse.com environment or have you transitioned to another provider like Heroku & mLab?
As a fellow Parse user with this same issue (background job failing with this error when performing many inserts), I look forward to any comments you may have.
I have a small afterSave trigger in my parse cloud code. The trigger just re-counts items that are flagged as deleted=false.
When I update my object, the count goes just fine, except when I update the deleted flag. It seems like the count is considering objects BEFORE the save event, which is not what I would expect from an "afterSave" trigger.
On the other hand, by using a "find" instead of a "count" on the very same query, I will always get the right number of objects. Of course the "find" way will go just when the query stays below 1000 objects.
Is there something wrong in my code? Is it the expected behavior? Is it a bug? Any alternative way to count objects the right way?
Thank you so much for your attention
Parse.Cloud.afterSave("BranchLike", function(request) {
console.log("Managing branchLike after save...");
Parse.Cloud.useMasterKey();
var branch = request.object.get("branch");
var branchLikeClass = Parse.Object.extend("BranchLike");
var type = request.object.get("type");
var branchLikeQuery = new Parse.Query(branchLikeClass);
branchLikeQuery.equalTo("branch",branch);
branchLikeQuery.equalTo("deleted",false);
branchLikeQuery.equalTo("type",type);
branchLikeQuery.count({
success: function(count) {
console.log("count = "+count); //works fine except on "delete" flag updates
/* ... */
},
error: function(error) {
/* ... */
}
});
branchLikeQuery.find({
success: function(branchLikes) {
console.log("ALTERNATIVE COUNT = "+branchLikes.length); //works fine when having less than 1000 objects
},
error: function(error) {
/* ... */
}
});
});
I have written a cloud code to change a Boolean value in table. The code is getting executed and the values are getting updated as well. But the issue is that I get the following error printed in my console. I am worried if this might cause a problem if the number of rows increase. Following is the error which is being printed on the console
I2015-09-15T06:15:48.317Z]v11: Ran job hello with:
Input: {}
Failed with: Error: Job status message must be a string
at updateJobMessageAndReturn (<anonymous>:1157:7)
at Object.success (<anonymous>:1211:9)
at e.obj.save.success (main.js:25:30)
at e.<anonymous> (Parse.js:12:27827)
at e.s (Parse.js:12:26759)
at e.n.value (Parse.js:12:26178)
at e.<anonymous> (Parse.js:12:26831)
at e.s (Parse.js:12:26759)
at e.n.value (Parse.js:12:26178)
at e.s (Parse.js:12:26887)
Following is the cloud code:
Parse.Cloud.job("hello", function(request, response) {
Parse.Cloud.useMasterKey();
var presentDate = new Date();
// presentDate.setDate(presentDate.getDate()-1);
presentDate.setHours(0,0,0,0);
var usersValid = new Parse.Query(Parse.User);
usersValid.equalTo("emailVerified", true);
//usersValid.greaterThan("updatedAt", presentDate);
var users = new Parse.Query("Properties");
users.matchesQuery("user",usersValid);
users.equalTo("verified", false);
users.limit(1000);
users.find({
success: function(results) {
console.log("Total new properties "+ results.length);
for (var i = 0; i < results.length; i++) {
var obj = results[i];
obj.set("verified", true);
obj.save(null,{
success: function (object) {
console.log("Success - "+i);
response.success(object);
},
error: function (object, error) {
console.log("Failed - "+i);
response.error(error);
}
});
}
},
error: function(error) {
console.log("failed");
}
});
When you call
response.success(object);
you're passing the full object that was just saved - but you shouldn't be. You can just call success with a simple status string, like 'OK', or with some element from the saved object, like its object id.
The more serious issue is that you're requesting 1000 items in the query and then updating and saving each individually - and in the save completion handler you're calling success or error. So, as soon as the first of those 1000 objects is saved you're telling the job it's complete and it can stop processing the rest.
You should change your job to use promises instead of old style callbacks and you should put all of the save promises into an array and wait for them to complete after your loop before you call success or error.
As many who came before me, I'm trying to run a bit of cloud code that will check for uniqueness and then insert or update the object as necessary.
The code correctly determines whether or not there is an existing object in the db with the same device token as the request.object
However, the existing object will not update it's countdownValue to 200.
I have tried adding and omitting the object.save() function
I have tried adding, omitting, and exchanging the response.error and response.success functions
The preexisting object remains untouched in all cases.
I have tried Updating existing Parse object in Cloud Code and many others.
Any help or thoughts would be greatly appreciated.
var Countdown = Parse.Object.extend("Countdown");
Parse.Cloud.beforeSave("Countdown", function(request, response) {
if (!request.object.get("devicetoken")) {
response.error('A Countdown must have a devicetoken.');
} else {
var query = new Parse.Query(Countdown);
query.equalTo("devicetoken", request.object.get("devicetoken"));
query.first({
success: function(object) {
if (object) {
object.set("countdownValue", "200");
object.save();
response.error("Failing on purpose");
}
else
{
response.success();
}
},
error: function(error) {
response.error("Could not validate uniqueness for this Countdown object.");
}
});
}
});