15

我想保存一个在 Parse 云代码中随机生成的registrationId,所以我需要检查该值是否已经在数据库中,我必须以递归方式执行此操作,直到获得正确的字符串。这是我到目前为止所尝试的,问题是 findRegistrationId() 不是一个承诺,所以我不能使用 then() 有没有办法让它成为一个承诺或任何其他解决方法?对于云代码

function getRandomString()

{
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
    var string_length = 4;
    var randomstring = '';

    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum + 1);
    }
    return randomstring;
}
function findRegistrationId()
{
    console.log("Enter findRegistrationId")
    var randomString = getRandomString();
    var query = new Parse.Query("Book");

    query.equalTo("registrationId", randomString);

    query.find.then(function(results){
        if(results.length === 0)
        {
            console.log("no registrationId duplicated")
            return randomString;
        }
        //if there is other registrationId we concatenate
        else
        {
            console.log("registrationId duplicated let's recursive it")
            return findRegistrationId();
        }
    },function(error){
        return error;
    })

}

// Use Parse.Cloud.define to define as many cloud functions as you want.
// Gets the unique cool BC identificator. The real glue of BC!
Parse.Cloud.define("GetBookId", function(request, response) {

    var promise = findRegistrationId();

    promise.then(function(result){

        console.log("success promise!!")
        response.success(result);

    }, function(error){
        console.error("Promise Error: " + error.message);

        response.error(error);

    });


});
4

2 回答 2

48

你可以这样写你的函数:

function findRegistrationId()
{
    console.log("Enter findRegistrationId")
    var randomString = getRandomString();
    var query = new Parse.Query("Book").equalTo("registrationId", randomString);

    var promise = new Parse.Promise();

    query.find().then(function(results) {
        if (results.length == 0)
        {
            promise.resolve(randomString);
        }
        else
        {
            findRegistrationId().then(function(result) {
                promise.resolve(result);
            }, function(error) {
                promise.reject(error);
            });
        }
    }, function(error) {
        promise.reject(error);
    });

    return promise;
}
于 2013-08-14T10:36:06.083 回答
18

@arghbleargh 发布了一个很好的解决方案,我只是发布了一个使用直接返回承诺而不是解决/拒绝的版本,这是一种反模式(参见https://github.com/petkaantonov/bluebird/wiki/Promise - 反模式例如):

function findRegistrationId()
{
    console.log("Enter findRegistrationId")
    var randomString = getRandomString();
    var query = new Parse.Query("Book").equalTo("registrationId", randomString);    

    return query.find().then(function(results) {
        if (results.length == 0)
        {
            return randomString;
            // or, you could return Parse.Promise.as(randomString);
            // but the explicit promise isn't required
        }
        else
        {
            findRegistrationId().then(function(result) {
                return result;
                // or, you could return Parse.Promise.as(result);
                // but the explicit promise isn't required
            }, function(error) {
                return Parse.Promise.error(error);
            });
        }
    }, function(error) {
        return Parse.Promise.error(error);
    });    
}
于 2015-10-05T02:11:11.077 回答