解析云代码嵌套查询时未调用代码141成功/错误消息



我一直收到一个代码141错误success/error未被调用。我正在运行另一个函数getCinemasInLocation,它返回类似于JSON的:{result:[result1,result2]}。我想对这个数组进行迭代,每次循环运行时都运行一个查询,并将所有结果传递给一个数组。也就是说,所有oterate的结果都将在一个数组中。我做得对吗?

//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");
            }
        });
    }
});

这是不起作用的getCinemasInLocation

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. Parse.Cloud.run不返回数组。它返回一个Promise。因此,在同一文件中创建一个普通的javascript函数:getCinemasInLocation()

  2. 正如@Delhi所说,您只能调用response.success()或response.error()一次。所以,不要把它们放在一个循环中。

  3. 并行使用Promises。因此,让我们使用Undercore的循环,而不是普通的FOR循环。您可以一次启动多个操作,然后使用Parse.Promise.when创建一个新的Promise,当它的所有输入Promise都被解析时,该Promise就会被解析。您可以在文档中阅读更多相关信息:https://www.parse.com/docs/js_guide#promises-平行

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

最新更新