【问题标题】:Why does chained function in Parse not work when I retrieve values from database?当我从数据库中检索值时,为什么 Parse 中的链式函数不起作用?
【发布时间】:2014-09-04 07:44:12
【问题描述】:

在下面的代码sn-p中,所有函数都在最后一个Parse Cloud代码函数中调用。当我使用 getCinemasInLocation 函数的注释部分对其进行测试时,它可以正常工作。也就是说,当我手动返回一个对象 ID 数组时。

但是,当我要求它从数据库中检索影院 ID 并将其用作函数中的返回值时,它会返回一个如下所示的空数组。

{"result":[[],[],[],[]]}

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

// 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("getMovieIdsInCinemass", 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 arrays function parse-platform cloud


    【解决方案1】:

    我解决了。我将整个函数包装在 getCinemasInLocation 的回调函数中。这就是调用函数现在的样子。

    Parse.Cloud.define("getMovieIdsInCinemass", function(request, response) {
    var cinemasInLocation = [];
    var theLocation = request.params.theLocation;
    
    getCinemasInLocation(theLocation, function(cinemas) {
    
    cinemasInLocation = cinemas;
    
    
    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);
        }
    );
    ///
    });
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2020-02-13
      • 2020-01-22
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多