I have a Parse Cloud Code function that will act as my background job. Right now I am in my debugging stage. The function does not throw an error and it is not doing what it is supposed to do. The function entitled "backgroundJob" is supposed to go through all the "Group" objects that have been created. Each "Group" object has an Array of "Event" objects, and all you have to know about the "Event" object is it has a date property called "date".
The function should go through all the events for each group object and if the event's date is the same as the current time the function should send a notification to all "Users" who are apart of that group and then delete that event. Right now the function is not deleting the event if it is the same time as the current time and is not notifying members of the group.
My function is below.
Parse.Cloud.define("backgroundJob", function(request, response) {
Parse.Cloud.useMasterKey();
var moments = require("cloud/moment.js");
moments().format();
var _ = require('cloud/underscore.js');
// Get the actual time, for use in non testing
// var now = moments();
// For testing edit time in ()
var now = moments("2015-23-11 19:30", "YYYY-MM-DD HH:mm");
var out = now;
console.log(out);
var groupObject = Parse.Object.extend("Group");
var query = new Parse.Query(groupObject);
var eventObject = Parse.Object.extend("Event");
query.find().then(function(groups) {
var promise = Parse.Promise.as();
var groupArray = groups;
for (var i = 0; i < groupArray.length; i++) {
promise = promise.then(function() {
var count = 0;
var eventArray = groupArray[i].get("Events");
for (count = 0; count < eventArray.length; count++) {
if (now == eventArray[count].get('date')) {
var curEvent = eventArray[count];
eventArray[count].destory();
var relationc = result.get("created");
var createdq = relationc.query();
var relationj = result.get("created");
var joinedq = relationj.query();
var partOnee = curEvent.get("name");
var outString = partOnee.concat(" is now");
Parse.Push.send({
where : createdq,
data : {
alert : outString
}
}).then(function() {
response.success();
}, function(error) {
response.error(error);
});
Parse.Push.send({
where : joinedq,
data : {
alert : outString
}
}).then(function() {
response.success();
}, function(error) {
response.error(error);
});
var e = eventArray[count];
var destroyp = Parse.Promise.as();
destroyp = promise.then(function() {
return e.destroy();
}, function(error) {
response.error(error);
});
}
}
});
}
}).then(function() {
response.success()
}, function(error) {
response.error(error);
});
});
Thank you.
For reference in the code there are two separate relations to users a "created" and a "joined", this is why in the code I have a created query and a reaction query.