Parse.com: Script error 'undefined' - javascript

I'm new to Parse.com cloud jobs and I have an error.
I need to make a cloud job that compute statistics out of all registered games in the database ( ~500K ).
My script first draft is :
Parse.Cloud.job("dostats", function(request, status) {
Parse.Cloud.useMasterKey();
var max_score_query = new Parse.Query("ScoreData");
max_score_query.descending('score');
max_score_query.first(
{
success: function(result) {
var stat_size = 40;
var count = 0;
var stats = new Array(stat_size);
for ( i = 0; i < stat_size; ++i) { stats[i] = 0; }
var max_score = result.get('score');
var promise = Parse.Promise.as();
promise = promise.then( function() {
var stats_query = new Parse.Query("ScoreData");
return stats_query.each( function(line) {
var score = line.get('score');
var id = parseInt((stat_size - 1) * ( score / max_score));
stats[id] = stats[id] + 1;
count = count + 1;
}).then( function() { status.success("lol"); },
function(error) { status.error("error"); });
});
return promise;
},
error: function() {
status.error("Unable to get max score");
}
});
});
});
If I console.log(stats.toString()) in the each loop data is correct.
However job fails with this message :
E2015-02-10T11:16:54.296Z] v137: Ran job dostats with:
Input: {}
Failed with: undefined
Any idea what I do wrong?

Related

How to callback an error message when searching a table is unsuccessful in DynamoDB

I'm currently using AWS Lambda JavaScript code to try and search a DynamoDB table this is then implemented into an Amazon Alexa application, but that isn't really important for what I'm asking. Here is the code I'm struggling with:
function readDynamoItem(params2, callback) {
var AWS = require('aws-sdk');
AWS.config.update({region: AWSregion});
var dynamodb = new AWS.DynamoDB();
console.log('reading item from DynamoDB table');
dynamodb.scan(params2, function (err, data){
if (err) {
callback("error");
//console.log(err, err.stack); // an error occurred
}
else{
callback(data);
}
});
}
So when an error occurs I want it to callback the message "error" and then use it here:
const params2 = {
TableName: 'Fixtures',
FilterExpression: 'team1 = :value',
ExpressionAttributeValues: {':value': {"S": MyQuestion.toLowerCase()}}
};
readDynamoItem(params2, myResult=>{
say = myResult;
this.response.speak(say).listen('try again');
this.emit(':responseReady');
});
All I'm getting at the moment is this response when I test, I think due to err just ending the program instead of calling the error back to use in the implementation:
Response:
{
"errorMessage": "RequestId: 0f586880-2ddb-11e8-bdf7-07b4c224b25d Process exited before completing request"
}
Any help would be greatly appreciated.
Here's the full code for my project for further reference:
const AWSregion = 'eu-west-1';
const Alexa = require('alexa-sdk');
const AWS = require('aws-sdk');
AWS.config.update({
region: AWSregion
});
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
// alexa.appId = 'amzn1.echo-sdk-ams.app.1234';
// alexa.dynamoDBTableName = 'YourTableName'; // creates new table for session.attributes
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'LaunchRequest': function () {
this.response.speak('welcome to magic answers. ask me a yes or no question.').listen('try again');
this.emit(':responseReady');
},
'MyIntent': function () {
var MyQuestion = this.event.request.intent.slots.MyQuestion.value;
console.log('MyQuestion : ' + MyQuestion);
const params2 = {
TableName: 'Fixtures',
FilterExpression: 'team1 = :value',
ExpressionAttributeValues: {':value': {"S": MyQuestion.toLowerCase()}}
};
const params3 = {
TableName: 'Fixtures',
FilterExpression: 'team2 = :value',
ExpressionAttributeValues: {':value': {"S": MyQuestion.toLowerCase()}}
};
readDynamoItem(params2, myResult=>{
var say = MyQuestion;
//if nothing is found when scanning for team1, scan team2
if (myResult == "error"){
readDynamoItem(params3, myResult2=>{
say = myResult2;
say = 'The top scorer for ' + MyQuestion + ' is ' + myResult2;
this.response.speak(say).listen('try again');
this.emit(':responseReady');
});
}
else{
say = myResult;
say = 'The top scorer for ' + MyQuestion + ' is ' + myResult;
this.response.speak(say).listen('try again');
this.emit(':responseReady');
}
});
},
'AMAZON.HelpIntent': function () {
this.response.speak('ask me a yes or no question.').listen('try again');
this.emit(':responseReady');
},
'AMAZON.CancelIntent': function () {
this.response.speak('Goodbye!');
this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
this.response.speak('Goodbye!');
this.emit(':responseReady');
}
};
// END of Intent Handlers {} ========================================================================================
// Helper Function =================================================================================================
//reading the Fixtures table
function readDynamoItem(params2, callback) {
var AWS = require('aws-sdk');
AWS.config.update({region: AWSregion});
var dynamodb = new AWS.DynamoDB();
var team1;
var team2;
console.log('reading item from DynamoDB table');
dynamodb.scan(params2, function (err, data){
if (err) {
callback("error");
//callback("error");
//console.log(err, err.stack); // an error occurred
}
else{
console.log(data); // successful response
team1 = jsonToString(data.Items[0].team1);
team2 = jsonToString(data.Items[0].team2);
var t1goals = jsonToString(data.Items[0].t1goals);
var t2goals = jsonToString(data.Items[0].t2goals);
t1goals = parseInt(t1goals);
t2goals = parseInt(t2goals);
var search;
var chosenValue = Math.random() < 0.5 ? team1 : team2;
// if goals are equal in a match then it is random which team will score next
if(t1goals == t2goals){
search = chosenValue;
}
//if a team has 1 goal more than the other then it is a 3rd more likely they will score next
else if(t1goals > t2goals && t1goals == 1){
if(randomInt(1, 3) == 1){
search = team2;
}
else{
search = team1;
}
}
else if(t2goals > t1goals && t2goals == 1){
if(randomInt(1, 3) == 1){
search = team1;
}
else{
search = team2;
}
}
//if a team has more than 1 goal more than the other then it is a 5th more likely they will score next
else if(t1goals > t2goals && t1goals > 1){
if(randomInt(1, 5) == 1){
search = team2;
}
else{
search = team1;
}
}
else if(t2goals > t1goals && t2goals > 1){
if(randomInt(1, 5) == 1){
search = team1;
}
else{
search = team2;
}
}
var params = {
TableName: 'yesno',
FilterExpression: 'team = :value',
ExpressionAttributeValues: {':value': {"S": search}}
};
readDynamoFixtures(params, myResult=>{
callback(myResult);
});
}
});
}
//read player details from the the yesno table
function readDynamoFixtures(params, callback) {
var goals = new Array();
var playing = new Array();
var messages = new Array();
var most = 0;
var mostMessage;
var dynamodb = new AWS.DynamoDB();
dynamodb.scan(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else{
for(var i = 0; i <= (data.Count - 1); i++){
console.log(data); // successful response
var temp = jsonToString(data.Items[i].playername);
messages[i] = temp;
temp = jsonToString(data.Items[i].goals);
temp = parseInt(temp);
goals[i] = temp;
temp = jsonToString(data.Items[i].playing);
playing[i] = temp;
//compare each players goals
if (goals[i] > most && playing[i] == "true"){
most = goals[i];
mostMessage = messages[i];
}
}
}
callback(mostMessage);
});
}
//convert database items from json format to string
function jsonToString(str){
str = JSON.stringify(str);
str = str.replace('{\"S\":\"', '');
str = str.replace('\"}', '');
return str;
}
//get a random int between min and max
function randomInt(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
Edit:
I have tried testing this code with .query instead of .scan and the error callback works perfectly which is strange but obviously for this implementation I need to use .scan
When you get the "Process exited" response from the Lambda it is helpful to log heavily to see where the Lambda is getting stuck and then check the Cloudwatch Logs to get to the detail.
Then you can pinpoint the exception and focus on it. At least for me, the root cause was many times unexpected as Lambdas force a different way of thinking.

JSOM dynamicly get two announcements from all lists in all webs

Need help with the chaining. The functions work. But async calls make it hard for me to get everything. Help me think right!
My thought:
Get All Webs recursively (function works)
Get all lists from webs and iff announcementlist add to array and pass along
Get two items from all announcmentlists and sort by created.
Add ALL announcement items into one large array (to be able to sort array later.
Heres the code,
function getAllWebs(success, error) {
var ctx = SP.ClientContext.get_current();
var web = ctx.get_site().get_rootWeb();
var result = [];
var level = 0;
result.push(web);
var getAllWebsInner = function (web, result, success, error) {
level++;
var ctx = web.get_context();
var webs = web.get_webs();
ctx.load(webs, 'Include(Title,Webs,ServerRelativeUrl)');
ctx.executeQueryAsync(
function () {
for (var i = 0; i < webs.get_count() ; i++) {
var web = webs.getItemAtIndex(i);
result.push(web);
if (web.get_webs().get_count() > 0) {
getAllWebsInner(web, result, success, error);
}
}
level--;
if (level == 0 && success)
success(result);
},
error);
};
getAllWebsInner(web, result, success, error);
}
function error(sender, args) {
console.log(args.get_message());
};
function getAnnouncementLists(web, success, error) {
var dfd = $.Deferred();
var ctx = web.get_context();
var collList = web.get_lists();
var result = []
ctx.load(collList, 'Include(Title, Id, BaseTemplate)');
ctx.executeQueryAsync(function () {
for (var i = 0; i < collList.get_count() ; i++) {
var list = collList.getItemAtIndex(i);
var bTemp = list.get_baseTemplate();
if (bTemp == 104) {
result.push(list);
}
}
//success(result);
dfd.resolve(result);
}, error);
return dfd.promise();
}
function getListItems(list, success, error) {
var dfd = $.Deferred();
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name="Created" Ascending="False"></FieldRef>'
+ '</OrderBy></Query><ViewFields><FieldRef Name="Title"/><FieldRef Name="Body"/>' +
'<FieldRef Name="Created"/></ViewFields><RowLimit>2</RowLimit></View>');
var listItems = list.getItems(camlQuery);
var result = []
var ctx = list.get_parentWeb().get_context();
ctx.load(listItems);
ctx.executeQueryAsync(function () {
for (var i = 0; i < listItems.get_count() ; i++) {
var item = listItems.getItemAtIndex(i);
result.push(item);
}
dfd.resolve(result);
//success(result);
}, error);
return dfd.promise();
}
function printResults(items) {
var sortedItems = items.sort(dynamicSort("get_created()"));
alert(sortedItems);
}
function dynamicSort(property) {
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
$(document).ready(function () {
var items = getAllWebs(
function (allwebs) {
var array = [];
for (var i = 0; i < allwebs.length; i++) {
getAnnouncementLists(allwebs[i]).then(function (announceLists) {
for (var i = 0; i < announceLists.length; i++) {
getListItems(announceLists[i]).then(function (items) {
array.push(items);
});
}
});
}
return array;
}
);
//getAllWebs(
// function (allwebs) {
// for (var i = 0; i < allwebs.length; i++) {
// getAnnouncementLists(allwebs[i],
// function (announceLists) {
// for (var i = 0; i < announceLists.length; i++) {
// getListItems(announceLists[i],
// function (items) {
// printResults(items);
// }, error);
// }
// }, error);
// }
// }, error);
});
Given the requirements to retrieve list items from Announcements lists located across site collection, below is demonstrated the modified example that contains some improvements such as:
the number of requests to the server is reduced
fixed the issue in getAllWebs function that prevents to return any results if site contains only a root web
Example
function getAllWebs(propertiesToRetrieve,success, error) {
var ctx = SP.ClientContext.get_current();
var web = ctx.get_site().get_rootWeb();
var result = [];
var level = 0;
ctx.load(web, propertiesToRetrieve);
result.push(web);
var getAllWebsInner = function (web, result, success, error) {
level++;
var ctx = web.get_context();
var webs = web.get_webs();
var includeExpr = 'Include(Webs,' + propertiesToRetrieve.join(',') + ')';
ctx.load(webs, includeExpr);
ctx.executeQueryAsync(
function () {
for (var i = 0; i < webs.get_count() ; i++) {
var web = webs.getItemAtIndex(i);
result.push(web);
if (web.get_webs().get_count() > 0) {
getAllWebsInner(web, result, success, error);
}
}
level--;
if (level == 0 && success)
success(result);
},
error);
};
getAllWebsInner(web, result, success, error);
}
function loadListItems(lists,query,success,error,results){
var results = results || [];
var curList = lists[0];
var ctx = curList.get_context();
var listItems = curList.getItems(query);
ctx.load(listItems);
ctx.executeQueryAsync(function () {
results.push.apply(results, listItems.get_data());
lists.shift();
if(lists.length > 0) {
loadListItems(lists,query,success,error,results);
}
if(lists.length == 0)
success(results);
}, error);
}
function dynamicSort(property) {
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
var propertiesToRetrieve = ['Lists.Include(BaseTemplate)','ServerRelativeUrl'];
getAllWebs(propertiesToRetrieve,
function(allwebs){
//1. get filtered lists
var allAnnouncementLists = [];
allwebs.forEach(function(w){
var announcementLists = w.get_lists().get_data().filter(function(l){
if(l.get_baseTemplate() == SP.ListTemplateType.announcements)
return l;
});
allAnnouncementLists.push.apply(allAnnouncementLists, announcementLists);
});
//2.Load list items from lists
var query = new SP.CamlQuery(); //<-set your custom query here
loadListItems(allAnnouncementLists,query,
function(allListItems){
//3.Sort and print results
var sortedItems = allListItems.sort(dynamicSort("get_created()"));
sortedItems.forEach(function(item){
console.log(item.get_item('Title'));
});
},logError);
},
logError);
function logError(sender,args){
console.log(args.get_message());
}

Send event from server to client to run a function

I looked at various answers but I can't not find a way to get this going for myself.
I have a function (in node.js) that selects a winner out of a pool, when it selects the winner though, I need it to send a event to the client where it runs a function with data. The data would be the array index of the winner.
Selecting a winner:
var endRound = function() {
ref.child('currentJackpot').once('value', function(data) {
var currentJackpot = data.val();
var winnerArray = [];
var winnerObj = {};
winnerObj.items = [];
for (var i = 0; i < currentJackpot.players.length; i++) {
winnerObj.items = winnerObj.items.concat(currentJackpot.players[i].items);
var playerValue = currentJackpot.players[i].itemsValue * 100;
currentJackpot.players[i].chance = ((currentJackpot.players[i].itemsValue / currentJackpot.jackpotValue) * 100).toFixed(2);
for (var j = 0; j < playerValue; j++) {
winnerArray.push(i);
}
}
var formatted = currentJackpot.roundHash.replace(/[.#$/]/g, "");
sgRef.child(formatted).once('value', function(data) {
var sgData = data.val();
salt = sgData.salt;
rngStr = sgData.rngStr;
console.log('ROUND ENDED! hash: ', hash, ' salt: ', salt, ' rngStr: ', rngStr);
currentJackpot.tickets = currentJackpot.jackpotValue * 100;
currentJackpot.winningTicket = Math.floor((parseFloat(rngStr, 2) * currentJackpot.tickets));
currentJackpot.winningNumber = (parseFloat(rngStr, 2) * 100).toFixed(2) + "%";
currentJackpot.winner = currentJackpot.players[winnerArray[currentJackpot.winningTicket]];
currentJackpot.salt = salt;
currentJackpot.rngStr = rngStr;
winnerObj.jackpotValue = currentJackpot.jackpotValue;
currentJackpot.jackpotValue = currentJackpot.jackpotValue.toFixed(2);
winnerObj.winner = currentJackpot.winner;
winnerObj.tradeToken = currentJackpot.winner.tradeToken;
ref.child('endedJackpots').push(currentJackpot);
bcrypt.genSalt(10, function(err, data) {
salt = data;
rngStr = JSON.stringify(rng());
bcrypt.hash(rngStr, salt, function(err, data) {
hash = data;
ref.child('currentJackpot').set({
itemsCount: 0,
jackpotValue: 0,
roundHash: hash,
}, function() {
console.log('NEW ROUND! hash: ', hash, 'salt: ', salt, 'rngStr: ', rngStr);
var formatted = hash.replace(/[.#$/]/g, "");
var sgJackpotRef = sgRef.child(formatted);
sgRef.set({}, function() {
sgJackpotRef.set({
salt: salt,
rngStr: rngStr,
}, function() {
request.post({
url: '*******',
body: winnerObj,
json: true,
}, function(error, response, body) {
if (error) {
console.log(error);
setPollTimer(10000);
} else {
usersRef.child(winnerObj.winner.id).once('value', function(data) {
var userData = data.val();
if (data.child('won').exists()) {
userData.won = (Math.floor(parseFloat(userData.won, 2)) + Math.floor(parseFloat(winnerObj.jackpotValue, 2))).toFixed(2);
} else {
userData.won = (Math.floor(parseFloat(winnerObj.jackpotValue, 2))).toFixed(2);
}
usersRef.child(winnerObj.winner.id).update({
won: userData.won
}, function() {
console.log('Added winnings to user data');
});
});
console.log('Making a withdraw request now to bot');
setPollTimer(10000);
}
});
});
});
});
});
});
});
});
};
And the function it should run client side:
function slotMachine(winnerIndex) {
var params = {
active: 3,
randomize: function(activeElementIndex){
return 1;
}
};
var machine = $('.slot').slotMachine( params );
$("#slotMachineButton").click(function(){
machine.shuffle(3, function(){
$(this).text("Index: " + this.active);
});
});
}
I'm completely stuck and stressed though, this isn't my code and It's a lot of code I can't grasp where to begin to do this. Any help?

Parse Cloud Code Error: Parse.Error {code: 141, message: "success/error was not called"}

I want to conditionally update several Parse.Objects using cloud code. The function is supposed to save select data to a new column should it apply to the User in question.
I'm having some serious issues with this in my first Cloud Code function. I think I have most of it right but I keep getting a success/error was not called error.
I'm using the following code:
Parse.Cloud.define("someFunction", function(request, response) {
var user = Parse.Object.extend("User");
var query = new Parse.Query(Parse.User);
var Table1 = Parse.Object.extend("Table1");
var table1Query = new Parse.Query(Table1);
var Table2 = Parse.Object.extend("Table2");
var table2Query = new Parse.Query(Table2);
var Ids = req.body //this is an array of Parse.Object Ids
var array = [];
var x = Ids.length
while (x--){
var Id = Ids[x];
table1Query.equalTo("user", {__type: "Pointer", className: "_User",
objectId: Id});
table1Query.find({
success: function (results) {
var resultIds = _.map(results, function (n) {
return n.id});
var resultObjs = _.map(results, function (n) {
return return _.extend(_.find(n), {id: n.id})});
var a = resultIds.length
while (a--) {
var resultId = resultIds[a];
table2Query.equalTo("user", {__type: "Pointer", className: "_User",
objectId: resultId});
table2Query.find({
success: function (items) {
var MA = _.map(_.flatten(items), function (n) {
return _.find(n)});
var step3 = _.map(resultObjs, function (n) {return _.extend(n, {
Matched: _.filter(MA, function (a) {return a.result.id == n.id})})});
var total = Math.round(_.reduce(_.map(step3, function (n) {return n.Bill
}), function (memo, num) {return memo + num;}, 0) * 100) / 100;
var duty = function (total, id) {
var promise = new Parse.Promise();
table2Query.get(id, {
success: function (Answer) {
Answer.set("duty", total);
Answer.save().then(function (difresult) {
response.success(difresult);
}, function (error) {
response.error(error);})
}
});
}
array.push(duty(Answer, Id))
}
})
}
}
})
}
return Parse.Promise.when(array);
})
I hope this is helpful to someone one day. The answer/difference in code is to be found at the very end as I added .then to put the response.success/error:
Parse.Cloud.define("someFunction", function(request, response) {
var user = Parse.Object.extend("User");
var query = new Parse.Query(Parse.User);
var Table1 = Parse.Object.extend("Table1");
var table1Query = new Parse.Query(Table1);
var Table2 = Parse.Object.extend("Table2");
var table2Query = new Parse.Query(Table2);
var Ids = req.body //this is an array of Parse.Object Ids
var array = [];
var x = Ids.length
while (x--){
var Id = Ids[x];
table1Query.equalTo("user", {__type: "Pointer", className: "_User",
objectId: Id});
table1Query.find({
success: function (results) {
var resultIds = _.map(results, function (n) {
return n.id});
var resultObjs = _.map(results, function (n) {
return return _.extend(_.find(n), {id: n.id})});
var a = resultIds.length
while (a--) {
var resultId = resultIds[a];
table2Query.equalTo("user", {__type: "Pointer", className: "_User",
objectId: resultId});
table2Query.find({
success: function (items) {
var MA = _.map(_.flatten(items), function (n) {
return _.find(n)});
var step3 = _.map(resultObjs, function (n) {return _.extend(n, {
Matched: _.filter(MA, function (a) {return a.result.id == n.id})})});
var total = Math.round(_.reduce(_.map(step3, function (n) {return n.Bill
}), function (memo, num) {return memo + num;}, 0) * 100) / 100;
var duty = function (total, id) {
var promise = new Parse.Promise();
table2Query.get(id, {
success: function (Answer) {
Answer.set("duty", total);
Answer.save().then(function (difresult) {
response.success(difresult);
}, function (error) {
response.error(error);})
}
});
}
array.push(duty(Answer, Id))
}
})
}
}
})
}
return
Parse.Promise.when(array).then(function() {
response.success("Successfully retrieved Total Bills.");
},
function(error) {
response.error("Something is still wrong");
console.log(error);
});;
})

Code 141 success/error message not called on parse cloud code nested query

I keep receiving a code 141 error success/error was not called. I am running another function getCinemasInLocation which returns a JSON like: {result: [result1, result2]}. I want to iterate over this array and run a query each time the loop runs and all the results to an array. That is, the results of all oteration will be in an array. Am I doing it right?
//This function uses getCinemasInLocation to retrieve the movie objects that are showing in the cinemas
Parse.Cloud.define("getMovieIdsInCinemas", function(request, response) {
var cinemasInLocaton = [];
var theLocation = request.params.theLocation;
cinemasInLocation = Parse.Cloud.run("getCinemasInLocation", {theLocation: theLocation});
for (i = 0; i < cinemasInLocation.length; i++){
var query = new Parse.Query("showing");
var movieIds = [];
query.equalTo("cinema", {
__type: "Pointer",
className: "Cinema",
objectId: cinemasInLocation[i]
});
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
movieIds.push(results[i].get("movie"));
}
response.success(movieIds);
},
error: function() {
response.error("movie lookup failed 2");
}
});
}
});
This is the getCinemasInLocation that does not work
function getCinemasInLocation(theLocation) {
// some code
//var result = ["xiUXXYFhAl","Yanh9iDykk"];
//return result;
var result = new Parse.Promise();
var query = new Parse.Query("Cinema");
query.equalTo("Location", theLocation);
query.find({
success: function(objects) {
var cinemas = [];
for (var i = 0; i < objects.length; i++) {
var cinema = objects[i];
cinemas.push(cinema.id);
}
result.resolve(cinemas);
},
error: function(error) {
result.reject(error);
}
});
return result;
}
Parse.Cloud.run doesn't return an array. It returns a Promise. So, create a normal javascript function in the same file: getCinemasInLocation()
As #Delhi said, you can only call response.success() or response.error() once. So, don't put them in a loop.
Use Promises on parallel. So, let's use the loop of Underscore instead of the normal FOR loop. You can start multiple operations at once, and use Parse.Promise.when to create a new promise that will be resolved when all of its input promises is resolved. You can read more about this in the documentation: https://www.parse.com/docs/js_guide#promises-parallel
var _ = require('underscore');
function getCinemasInLocation(theLocation) {
// some code
var result = [id1, id2];
return result;
}
// This function returns the array of movieIds of a cinema
function getMovieIdsInCinema(cinemaId) {
var result = new Parse.Promise();
var query = new Parse.Query("showing");
query.equalTo("cinema", {
__type: "Pointer",
className: "Cinema",
objectId: cinemaId
});
query.find({
success: function(objects) {
var movieIds = [];
for (var i = 0; i < objects.length; i++) {
var movie = objects[i].get("movie");
movieIds.push(movie.id);
}
result.resolve(movieIds);
},
error: function(error) {
result.reject(error);
}
});
return result;
}
Parse.Cloud.define("getMovieIdsInCinemas", function(request, response) {
var cinemasInLocation = [];
var theLocation = request.params.theLocation;
cinemasInLocation = getCinemasInLocation(theLocation);
var promises = [];
_.each(cinemasInLocation, function(cinemaId) {
promises.push(getMovieIdsInCinema(cinemaId));
});
Parse.Promise.when(promises).then(
function() {
var result = [];
_.each(arguments, function(object) {
result.push(object); // each object is an array of movieIds
});
response.success(result); // return array of arrays
},
function(error) {
response.error(error);
}
);
});

Categories