Search code examples
javascriptparse-platform

Parse database javascript "OR" query


I'm using Parse backend for my javascript application. It's for a shop and the cashier will need to scan a QR code, then a list of products on that card will load. I want to load the information from every product on that card. I get that with different queries and then 1 mainQuery to get them. I'm testing with 3 products, now I'm doing this:

var mainQuery = Parse.Query.or(queries[0], queries[1], queries[2]);

I set the queries with a for loop but this line above me needs to be flexible. Now it's just for 3 products. The ideal thing to do would be:

var mainQuery = Parse.Query.or(queries);

Just giving all the queries at once... but this doesn't work.. Is there any solution to get this to work?

Thanks in advance!


Solution

  • I do not know parse.com at all, but to generalize this as a pure javascript answer any function which takes multiple parameters can be called with apply and an array

    function doSomething(arg1, arg2, arg3){
        console.log(arg1,arg2,arg3);
    }
    
    var arr = ["val1","val2","val3"]
    doSomething.apply(this, arr);

    So, in your question you can probably do this:

    var mainQuery = Parse.Query.or.apply(Parse.Query,queries);
    

    There is an even easier way to do this in ES6, using the spread syntax

    var mainQuery = Parse.Query.or(...queries);