0

I have a "Product" collection that has a pointer reference to a "Category" collection that I include on a product query. How do I query on this category value, without first querying the category itself separately?

var productQuery = new Parse.Query("Product");
productQuery.include("category");

if(params.category) {
    productQuery.equalTo("category", params.category);
}

...

productQuery.find({
    success: function(products) {...},
    error: function(err) {...}
});

what do not want:

var productQuery = new Parse.Query("Product");
productQuery.include("category");

if(params.category) {
    var categoryQuery = new Parse.Query("Category");
    categoryQuery.equalTo("name", params.category);
    categoryQuery.first({
        success: function(foundCategory) {
            productQuery.equalTo("category", foundCategory);
        }
    });

    // Yes, I am aware that in this example the product query below would likely be executed before the category query above finishes.
}

...

productQuery.find({
    success: function(products) {...},
    error: function(err) {...}
});
Carel
  • 2,063
  • 8
  • 39
  • 65

1 Answers1

0

What you're looking for is the matchesQuery or matchesKeyInQuery methods on a Parse.Query: http://parseplatform.org/Parse-SDK-JS/api/classes/Parse.Query.html#methods_matchesQuery

It'll basically do what you're doing under the hood except more efficiently and a lot cleaner on the high level code you write.

Jake T.
  • 4,308
  • 2
  • 20
  • 48