Skip to content

Instantly share code, notes, and snippets.

@fxp
Created March 13, 2015 04:25
Show Gist options
  • Save fxp/7e384eaa46dc3102f6c7 to your computer and use it in GitHub Desktop.
Save fxp/7e384eaa46dc3102f6c7 to your computer and use it in GitHub Desktop.
Get all possible value of a certain field from Leancloud
var getAllValues = function (className, fieldName) {
var promise = new AV.Promise();
var TargetClass = AV.Object.extend(className);
var values = [];
var getNextValue = function () {
var query = new AV.Query(TargetClass);
query.notContainedIn(values);
query.first().then(function (obj) {
if (obj) {
values.push(obj.get(fieldName));
getNextValue()
} else {
promise.resolve(values);
}
})
}
return promise;
}
@hihell
Copy link

hihell commented Mar 13, 2015

var getAllValues = function (className, fieldName) {
var promise = new AV.Promise();

var TargetClass = AV.Object.extend(className);
var values = [];
var getNextValue = function () {
    var query = new AV.Query(TargetClass);
    query.notContainedIn(fieldName, values);
    query.first().then(function (obj) {
        if(obj) {
            values.push(obj.get(fieldName));
            getNextValue()
        } else {
            values.sort()
            promise.resolve(values);
        }
    }, function (err) {
        promise.reject(err);
    })
}
getNextValue();
return promise;

}
求PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment