1

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;
}

1 Answer 1

1
  1. Parse.Cloud.run doesn't return an array. It returns a Promise. So, create a normal javascript function in the same file: getCinemasInLocation()

  2. As @Delhi said, you can only call response.success() or response.error() once. So, don't put them in a loop.

  3. 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); } ); });
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. It worked perfectly. I edited the getCinemasinLocation function to retrieve the cinemas from the database and then it returns a null array. I will post the edit above. I replicated the getMovieIdsInCinema() in doing it, and it still does not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.