Skip to content

Instantly share code, notes, and snippets.

@adelevie
Created September 14, 2012 04:49
Show Gist options
  • Save adelevie/3719861 to your computer and use it in GitHub Desktop.
Save adelevie/3719861 to your computer and use it in GitHub Desktop.
// implementation of random queries using Parse Cloud Code
// see https://parse.com/questions/random-search-its-possibile for setup details
Parse.Cloud.define('randomNouns', function(request, response) {
var NounMaster = Parse.Object.extend("NounMaster");
var maxQuery = new Parse.Query(NounMaster);
maxQuery.first({
success: function(object) {
var max = object.get("nextWordIndex");
var n = 10;
if(request.params.count) { n = request.params.count; }
var arr = [];
while (arr.length < n) {
arr.push(Math.ceil(Math.random() * max));
}
var indexes = arr;
var Noun = Parse.Object.extend("Noun");
var nounQuery = new Parse.Query(Noun);
nounQuery.containedIn("index", indexes);
nounQuery.find({
success: function(results) {
response.success(results);
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment