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

标签 javascript json parse-platform cloud parse-cloud-code

我不断收到代码 141 错误成功/错误未调用。我正在运行另一个函数 getCinemasInLocation,它返回一个 JSON,例如:{result: [result1, result2]}。我想迭代这个数组,并在每次循环运行时运行一个查询,并将所有结果保存到一个数组中。也就是说,所有操作的结果都将在一个数组中。我这样做对吗?

//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. 并行使用 Promise。因此,让我们使用 Underscore 循环代替普通的 FOR 循环。您可以一次启动多个操作,并使用 Parse.Promise.when 创建一个新的 Promise,该新 Promise 将在其所有输入 Promise 都得到解决时得到解决。您可以在文档中阅读更多相关信息:https://www.parse.com/docs/js_guide#promises-parallel

var _ = require('underscore');<p></p>

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

关于javascript - 解析云代码嵌套查询时未调用代码 141 成功/错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25564704/

相关文章:

sql - 通过 BigQuery 中的联接构建多级嵌套结构(使用嵌套和重复字段)

javascript - 解析预定的后台作业

arrays - Swift 中解析 block 的基础知识

swift - Superfeedr 无法访问 Parse.com 回调 URL

javascript - 当图像可能尚未加载时,获取其中包含图像的元素的 $.height()

javascript - html 5 canvas javascript 下雨动画(如何高效轻松地实现!)

javascript - 用于测试字符串、数字和特殊字符的 javascript 正则表达式

javascript - 显示 NaN 输出的 Js 表达式

java - 如何在从 Java 对象解析的 json 数组中添加 Json header

c# - C#是否具有用于将JSON文本转换为可管理对象的内置对象,还是我需要第三方库?