Search code examples
javascriptparse-platformparse-server

Parse or query with Array


I'm trying to create an or query from an array of queries. I need to use an array because the amount of queries isn't fixed, it depends on a user input.

This is some sample code from parse.com to implement an or query.

var lotsOfWins = new Parse.Query("Player");
lotsOfWins.greaterThan("wins", 150);

var fewWins = new Parse.Query("Player");
fewWins.lessThan("wins", 5);

var mainQuery = Parse.Query.or(lotsOfWins, fewWins);
mainQuery.find()
  .then(function(results) {
    // results contains a list of players that either have won a lot of games or won only a few games.
  })
  .catch(function(error) {
    // There was an error.
  });

What I'm trying to do is

var orQueries = [];

Filling the array within a for-loop with Parse.Query Objects. Then I'm calling

var userQuery = new Parse.Query.or(orQueries);

which causes a internal server error:

"TypeError: A ParseQuery must be constructed with a ParseObject or class name.

How can I solve this issue and initialize the query with an array?


Solution

  • One answer is to use JS to apply the arguments like this...

    // In older JS:
    var orQueries = [lotsOfWins, fewWins];
    var querySum = Parse.Query.or.apply(null, orQueries);
    
    // >=ES6:
    var orQueries = [lotsOfWins, fewWins];
    var querySum = Parse.Query.or(...orQueries);
    

    I don't know the upper limit for the length of the query array, but you should take care not to put that fully under user control.

    EDIT - If the queries are few in number and produce reasonably small results sets (like under 1k total?) you could forgo Query.or and do the "or" work yourself...

    const _ = require('underscore');
    
    function disjunctiveQuery(queries) {
        return Promise.all(queries).then(results => _.uniq(_.flatten(results), 'id'));
    }
    
    disjunctiveQuery([queryA, queryB, queryC, queryD]).then( ...