Parse cloud code ascending / limit won't work in query in promise

泪湿孤枕 提交于 2019-12-11 04:09:23

问题


In the following cloud code, the first query works fine.

In the chained query, if you include the two lines of code:

query.ascending("createdAt");       // NOTE
query.limit(5);                     // NOTE

it does not work! if you comment out those two lines of code, it works great. What could be the problem?

It throws no errors, but simply does not execute the .each at all. If you comment out the two lines of code in question, the .each executes perfectly for all results found.

Parse.Cloud.define("someFunction", function(request, response)
{
    var query = new Parse.Query("Club");
    query.equalTo("objectId", request.params.club);
    query.first(
    {
        success: function(object)
        {
            return object;
        },
        error: function(error) {...}

    }).then(function(club)
    {
        var query = new Parse.Query("Player");
        query.equalTo("club", club);
        query.include("user");
        query.ascending("createdAt");       // NOTE
        query.limit(2);                     // NOTE
        query.each(function(employee)
        {
            var teste = employee.get("email")
            console.log("teste  ... "  + teste);
        }) ...

回答1:


I'm surprised it works with our without the sort and limit qualifications. (I don't see how you got to chain .then() off the callback version of first()). The code can be cleaned up and made to work -- I think -- by restricting yourself to the promise-returning variety of the parse methods.

I also recommend factoring into small, promise-returning functions for each logical chunk. With that...

// return a promise fulfilled with a Club object whose id is 'clubId'
function clubWithId(clubId) {
    var query = new Parse.Query("Club");
    return query.get(clubId);  // prettier than "objectId" equalTo and first()
}

// return a promise fulfilled with up to 'limit' Players belonging to 'club' object
function playersWithClub(club, limit) {
    var query = new Parse.Query("Player");
    query.equalTo("club", club);
    query.include("user");
    query.ascending("createdAt"); 
    query.limit(limit);
    return query.find();
}

With these tidy, testable individual parts, we can more confidently build the cloud function as follows...

// a great toolbox for dealing with collections and other stuff
var _ = require('underscore');

Parse.Cloud.define("someFunction", function(request, response) {
    var clubId = request.params.club;
    clubWithId(clubId).then(function(club) {
        return playersWithClub(club, 2);
    }).then(function(players) {
        _.each(players, function(player) {
            var user = player.get("user");
            console.log("username is: " + user.username);
        });
        response.success(players);
    }, function(error) {
        response.error(error);
    });
});

Notice how we get from promise to promise? By returning the object we want as input to the next. Also notice that the chain can have a single rejection function at the end, or intervening ones so long as we follow the rule of returning the results from the resolutions.



来源:https://stackoverflow.com/questions/32429573/parse-cloud-code-ascending-limit-wont-work-in-query-in-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!