Parse.Cloud.job("JobSchedule", function(request, response) {
var Group = Parse.Object.extend("Group");
var query = new Parse.Query(Group);
query.equalTo("JobScheduled", true);
query.find({
success: function(results) {
for (var i = 0; i < results.length; ++i) {
var created = new Date(results[i].createdAt);
var current = new Date();
var timeDiff = Math.abs(current.getTime() - created.getTime());
var horsDiff = timeDiff / (60 * 60 * 1000);
if (horsDiff >= parseInt(results[i].get("JobHours"))) {
results[i].set("JobScheduled", false);
results[i].set("GroupType", "Private");
results[i].set("JobHours", 0);
results[i].save();
var GroupMembers = Parse.Object.extend("GroupMembers");
var query1 = new Parse.Query(GroupMembers);
query1.equalTo("GroupId", results[i].id);
query1.find({
success: function(grpresults) {
for (var j = 0; j < grpresults.length; ++j) {
grpresults[j].set("GroupType", "Private");
grpresults[j].save();
}
},
error: function(error) {
response.error(error);
}
})
}
}
},
error: function(error) {
response.error(error);
}
});
});
Script is running properly(it updates the values as defined). But in parse log it shown as Failed with: success/error was not called. Please suggest me over this issue
You simply never call response.success() or response.error(), and you should always call at least one of those.
You should really be storing all of the promises returned from your queries and saves and waiting until all of them are complete and then calling success or error based on the result from those promises.
Related
I am getting below error after updating to parse's latest release.
Result: Error: Cannot create a pointer to an unsaved ParseObject
at n.value (Parse.js:12:4222)
at n (Parse.js:14:607)
at Parse.js:14:374
at Array.map (native)
at n (Parse.js:14:351)
at Object.r.default (Parse.js:14:1071)
at Object.b._encode (Parse.js:11:15607)
at Object.success (<anonymous>:156:42)
at e.rQuery.find.success (main.js:279:22)
at e.<anonymous> (Parse.js:12:27827)
for below code
rQuery.equalTo("rNumber", r2Number);
rQuery.descending("rDateTime");
rQuery.find({
success: function(results) {
results = editListForSectionHeader(results);
response.success(results);
},
error: function(error) {
// on error, log it
console.log(error.message);
response.success([]);
}
function editListForSectionHeader(results){
var previousDate = '';
var formatedResults = [];
for (var i = 0; i < results.length; i++) {
var object = results[i];
if(previousDate != object.get('rDate')){
var headerRow = new R();
headerRow.set("rDate", object.get('rDate'));
headerRow.set("rowType", "HEADER_ROW");
formatedResults.push(headerRow);
object.set("rowType", "DATA_ROW");
formatedResults.push(object);
previousDate = object.get('rDate');
headerRow.dirty = function() { return false; };
object.dirty = function() { return false; };
}else{
object.set("rowType", "DATA_ROW");
object.dirty = function() { return false; };
formatedResults.push(object);
}
}
return formatedResults;
}
Basically I am getting some data with query, formatting the result array, setting dirty properties to false and returning. It use to work but it stopped with the new version.
Hi while I run this Job in Cloud code I get an error log:
Failed with: success/error was not called. Define functions working good, but in Job logs I have this error log. Please assist me to solve the issue
Parse.Cloud.job("JobSchedule", function (request, response) {
var Group = Parse.Object.extend("Group");
var query = new Parse.Query(Group);
query.equalTo("JobScheduled", true);
query.find({
success: function (results) {
for (var i = 0; i < results.length; ++i) {
var created = new Date(results[i].createdAt);
var current = new Date();
var timeDiff = Math.abs(current.getTime() - created.getTime());
var horsDiff = timeDiff / (60 * 60 * 1000);
if (horsDiff >= parseInt(results[i].get("JobHours"))) {
results[i].set("JobScheduled", false);
results[i].set("GroupType", "Private");
results[i].set("JobHours", 0);
results[i].save();
var GroupMembers = Parse.Object.extend("GroupMembers");
var query1 = new Parse.Query(GroupMembers);
query1.equalTo("GroupId", results[i].id);
query1.find({
success: function (grpresults) {
for (var j = 0; j < grpresults.length; ++j) {
grpresults[j].set("GroupType", "Private");
grpresults[j].save();
}
},
error: function (error) {
response.error(error);
}
});
}
}
},
error: function (error) {
response.error(error);
}
});
});
In each of your success scenarios, you need to call response.success(), where between the parenthesis you can return a status message, such as
response.success('Hello world did work');
In your case, this would probably go here:
Parse.Cloud.job("JobSchedule", function (request, response) {
var Group = Parse.Object.extend("Group");
var query = new Parse.Query(Group);
query.equalTo("JobScheduled", true);
query.find({
success: function (results) {
for (var i = 0; i < results.length; ++i) {
var created = new Date(results[i].createdAt);
var current = new Date();
var timeDiff = Math.abs(current.getTime() - created.getTime());
var horsDiff = timeDiff / (60 * 60 * 1000);
if (horsDiff >= parseInt(results[i].get("JobHours"))) {
results[i].set("JobScheduled", false);
results[i].set("GroupType", "Private");
results[i].set("JobHours", 0);
results[i].save();
var GroupMembers = Parse.Object.extend("GroupMembers");
var query1 = new Parse.Query(GroupMembers);
query1.equalTo("GroupId", results[i].id);
query1.find({
success: function (grpresults) {
for (var j = 0; j < grpresults.length; ++j) {
grpresults[j].set("GroupType", "Private");
grpresults[j].save();
}
// HERE IS THE NEW CODE
response.success("Saved objects properly");
},
error: function (error) {
response.error(error);
}
});
}
}
error: function (error) {
response.error(error);
}
});
});
I have the following code where I am trying to save personalities of a movie. Only few personalities get created and saved.
var query = new Parse.Query(Movie);
query.find({ success: function(movies) {
console.log("movies.length " + movies.length );
for (var movieIterator = 0; movieIterator < movies.length ; movieIterator++) {
cast_array = movies[movieIterator].get("cast");
console.log(cast_array);
for (var i = 0; i < cast_array.length; i++) {
var personalityObj = new Personality();
personalityObj.set('name', cast_array[i].trim());
personalityObj.save(); // NOt all personalities get saved
}
}
response.success("Awesome");
}, error: function(error) {
response.error(error)
}});
How may I improve the code so that all the personalities get stored/saved in the DB.
Can you try this and let me know if it works.
var query = new Parse.Query(Movie);
query.find({ success: function(movies) {
console.log("movies.length " + movies.length );
var list = [];
for (var i = 0; i < movies.length ; i++) {
cast_array = movies[i].get("cast");
console.log(cast_array);
for (var j = 0; j < cast_array.length;j++) {
var personalityObj = new Personality();
personalityObj.set('name', cast_array[j].trim());
list.push(personalityObj);
}
}
Parse.Object.saveAll(list).then(function(results){
console.log("Objects were saved!");
response.success("Awesome");
},function(eerror){
console.log(eerror);
response.error(eerror);
});
}, error: function(error) {
response.error(error);
}});
The above code is using saveAll function for batch operation. Docs
I have defined a cloud function to set custom column in User table. I have 6 results returned for my query but update of field only happens for 3 rows, I could not understand what's wrong with the code below,
Parse.Cloud.define("updateSaturdayAbsentWeeks", function(request, response) {
// User master key so that we can update all users
Parse.Cloud.useMasterKey();
// Tables to Query
var query = new Parse.Query(Parse.User);
// We want only players who are absent on Saturdays
query.equalTo("isSaturdayAbsent", true);
query.find({
success: function(results) {
console.error('updateSaturdayAbsentWeeks, Saturday absent players count ' + results.length);
for (var i = 0; i < results.length; i++) {
var absentUser = results[i];
var absentWeeks = absentUser.get('saturdayAbsentWeeks') - 1;
absentUser.set("saturdayAbsentWeeks", absentWeeks);
absentUser.save();
console.error('updateSaturdayAbsentWeeks, absentWeeks for user name = ' + absentUser.get('username') + ', is = ' + absentWeeks)
}
// All done
response.success("updateSaturdayAbsentWeeks finished successfully");
},
error: function() {
console.error("runSaturdayExpense, lookUp Failed - Each player enjoyed the game on Saturday ");
response.error("updateSaturdayAbsentWeeks failed");
}
});
});
After the query I can see it returned 6 records but in the for loop it only updates 3 records only and does not do anything to other three records and no errors returned.
Any pointers to understand why all 6 records are not getting updated would be greatly appreciated.
Thanks
This has to do with the fact that the save() function is asynchronous. The proper way to do this is using saveAll().
query.find({
success: function(results) {
var saveThese = [];
for (var i = 0; i < results.length; i++) {
var absentUser = results[i];
var absentWeeks = absentUser.get('saturdayAbsentWeeks') - 1;
absentUser.set("saturdayAbsentWeeks", absentWeeks);
saveThese.push(absentUser);
}
Parse.Object.saveAll(saveThese, {
success: function (list) {
response.success("updateSaturdayAbsentWeeks finished successfully");
},
error: function (error) {
response.error("did not save all the items");
}
});
},
error: function() {
response.error("updateSaturdayAbsentWeeks failed");
}
});
So I'm trying to find a method of getting a progress bar working on my .ajax call but not having much luck. I know that the ajaxForm plugin has the following code in it that allows for the uploadProgress option but the way my code works I'm not able to use that plugin. Is there anyway of adding the following code somehow so that it attaches to the standard .ajax call? Long shot I know!
// XMLHttpRequest Level 2 file uploads (big hat tip to francois2metz)
function fileUploadXhr(a) {
var formdata = new FormData();
for (var i=0; i < a.length; i++) {
formdata.append(a[i].name, a[i].value);
}
if (options.extraData) {
var serializedData = deepSerialize(options.extraData);
for (i=0; i < serializedData.length; i++)
if (serializedData[i])
formdata.append(serializedData[i][0], serializedData[i][1]);
}
options.data = null;
var s = $.extend(true, {}, $.ajaxSettings, options, {
contentType: false,
processData: false,
cache: false,
type: method || 'POST'
});
if (options.uploadProgress) {
// workaround because jqXHR does not expose upload property
s.xhr = function() {
var xhr = jQuery.ajaxSettings.xhr();
if (xhr.upload) {
xhr.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position; /*event.position is deprecated*/
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
options.uploadProgress(event, position, total, percent);
}, false);
}
return xhr;
};
}
s.data = null;
var beforeSend = s.beforeSend;
s.beforeSend = function(xhr, o) {
o.data = formdata;
if(beforeSend)
beforeSend.call(this, xhr, o);
};
return $.ajax(s);
}
I do not guarantee on that, but try this:
xhr.upload.onprogress = function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
console.log("Progress: "+percent+"%"); //debug to see if the problem is there
options.uploadProgress(event, position, total, percent);
};
From posted code, I cannot even guess if the problem is in onprogress event handling or that options.uploadProgress, whatever it is.
I posted this as answer only because it wouldn't fit in comment.