解析云代码的升序/限制在诺言中的查询中不起作用 [英] Parse cloud code ascending / limit won't work in query in promise

查看:68
本文介绍了解析云代码的升序/限制在诺言中的查询中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的云代码中,第一个查询工作正常.

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?

它不会引发任何错误,但根本不会执行.each.如果您注释掉这两行代码,则.each对于找到的所有结果都将完美执行.

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

推荐答案

我很惊讶它可以与我们的无排序和限制条件一起使用. (我看不到如何将.then()链接到first()的回调版本中).我认为,可以通过限制自己使用返回承诺的各种解析方法来清理代码并使之工作.

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.

这篇关于解析云代码的升序/限制在诺言中的查询中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆